Covariant return type

Aarti_pl aarti at interia.pl
Thu Mar 22 02:55:47 PDT 2007


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".

I don't know if it is bug or enhancement or even if it has any sense :-).

Please answer what you think...

BR
Marcin Kuszczak
(aarti_pl)


More information about the Digitalmars-d-learn mailing list