Automatic return type covariance for functions that return this?
Ben Davis
entheh at cantab.net
Sat Sep 15 14:57:45 PDT 2012
Never mind, I found the answer in the 'templates' page of the spec:
class Super {
T doStuff(this T)() { ...; return cast(T)this; }
}
Sub x = (new Sub()).doStuff();
It seems a bit of a shame that I need the cast, but it's a small thing :)
On 15/09/2012 22:53, Ben Davis wrote:
> 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