Covariant return type

Jarrett Billingsley kb3ctd2 at yahoo.com
Thu Mar 22 07:33:21 PDT 2007


"Aarti_pl" <aarti at interia.pl> wrote in message 
news:ettjqu$14jb$1 at digitalmars.com...
> Hello!
>
> Shouldn't code below work?:
>
> //-------------------------------------------
>
> abstract class Storage {
>     Storage get() {
>         return this;
>     }
> }
>
> class SpecificStorage : Storage {
>     void print() {}
> }
>
> void main() {
>     SpecificStorage s = (new SpecificStorage).get();
>     s.print;
> }
>
> //-------------------------------------------
>
> Unfortunately currently you have to add overridden implementation of get() 
> in SpecificStorage, like below (what is a little bit tedious work):
>
>     SpecificStorage get() {
>         return this;
>     }
>
>
> But my intuition about that would be that "this" pointer from method get 
> from Storage class will point to "SpecificStorage" when there is 
> instantiated SpecificStorage. But it looks that in fact this points to 
> "Storage".

Sounds like it's behaving correctly to me.  When you call .get on a 
SpecificStorage, the only override of it is the one that returns a Storage 
in the base class, and you can't cast from Storage to SpecificStorage 
implicitly.  The language will not implicitly define covariantly returning 
versions of your methods for you; that's up to you. 




More information about the Digitalmars-d-learn mailing list