Automatic return type covariance for functions that return this?

Ben Davis entheh at cantab.net
Sat Sep 15 14:53:12 PDT 2012


Hi,

Is it possible in D to achieve this effect:

class Super {
   typeof(this) doStuff() { ...; return this; }
}

class Sub : Super {
   //doStuff is NOT explicitly overridden here
}

Sub x = (new Sub()).doStuff();

The last line doesn't compile because doStuff() returns Super. Is there 
a way to make it return Sub without having to explicitly override the 
function?

Thanks in advance,

Ben :)


More information about the Digitalmars-d-learn mailing list