Rainer Deyke wrote:
Andrei Alexandrescu wrote:
interface Cloneable(T) if (is(T == class))
{
    private T doClone(); // must implement but can't call
    T clone()            // this is what everybody can call
    {
        auto result = doClone();
        assert(typeof(result) == typeof(this));
        assert(this.equals(result));
        return result;
    }
}

This sounds like a case for contract inheritance rather than two layers
of functions.

Contract inheritance has its place but NVI is more general than that.

interface ComparableForEquality(T)
{
    protected bool doEquals(T);
    final bool equals(T rhs)
    {
        auto result = doEquals(rhs);
        assert(rhs.equals(cast(T) this) == result);
        return result;
    }
}

This, on the other hand, does require two layers of functions if you
want to remove the infinite recursion by replacing the 'equals' in the
assertion with 'doEquals'.

Oops, yah, sorry about that. Replace rhs.equals with rhs.doEquals. Access to doEquals was actually the point of the example.


Andrei

Reply via email to