equivariant functions

Benji Smith dlanguage at benjismith.net
Tue Oct 14 18:59:55 PDT 2008


Steven Schveighoffer wrote:
> class A : IClonable
> {
>   typeof(this) clone() const { return new A();}
> }
> 
> class B : A
> {
> }
> 
> B b = new B;
> B x = b.clone();
> 
> Oops, b.clone() really only returns A, so this should fail.  The real 
> signature in A should be:

No, b.clone() definitely returns an instance of B. It's purely 
coincidental (from the caller's perspective) that B's implementation is 
identical to that of A.

Though I always thought the correct implementation of ICloneable was 
like this:

    interface ICloneable(T) {
       T clone();
    }

    A : ICloneable!(A) {
       public A clone() { ... }
    }

I really don't see what all the fuss is about with the equivariance 
stuff, except that it introduces a lot of confusing rules to fix the 
holes in the (already complex and confusing) const system.

Any other use case where equivariance might apply (other than the const 
stuff) seems to already have a simple, straightforward solution using 
either templates, interfaces, or plain old overloads.

--benji



More information about the Digitalmars-d mailing list