equivariant functions
Steven Schveighoffer
schveiguy at yahoo.com
Tue Oct 14 19:19:50 PDT 2008
"Benji Smith" wrote
> 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.
B is definitely not the same as A. B could have a different implementation,
I just didn't give it one.
e.g.:
class A : IClonable
{
typeof(this) clone() const { return new A(); }
void foo() {writefln("This is A");}
}
class B : A
{
void foo() {writefln("This is B");}
}
B b = new B;
b.clone().foo(); // prints "This is A"
It can't work any other way, or else the type system would be broken.
-Steve
More information about the Digitalmars-d
mailing list