The Non-Virtual Interface idiom in D
Rainer Deyke
rainerd at eldwood.com
Fri Sep 25 20:33:03 PDT 2009
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.
> 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'.
--
Rainer Deyke - rainerd at eldwood.com
More information about the Digitalmars-d
mailing list