Non-covariance and overrides

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Mon Sep 2 08:00:32 PDT 2013


Hello all,

In my graph library, I have a couple of different graph class implementations 
which, ideally, might better be implemented as a base class and a subclass.  The 
internal data storage of the latter is a superset of the other; many of the 
functions involve simply extra lines added to the code of what would be the base 
class.  See:
http://forum.dlang.org/post/mailman.587.1377784964.1719.digitalmars-d-learn@puremagic.com

... for some details.

However, while the essentials of the API are the same, some of the functions 
have different return types -- say, an array instead of a range.  This makes 
overrides impossible, as the different return types are not covariant (that is, 
one is not a subclass of the other).

Is there any way to deal with this situation and simply insist that the 
subclass's function definition replaces the base class's?  I'm not concerned 
about the sides of inheritance such as trying to disguise the subclass as an 
instance of the base class; it's fine that the API simply gets overwritten 
rather than overridden (if you get me).

Any ideas?

Thanks & best wishes,

     -- Joe


P.S. If I don't find a handy inheritance-based way to do this, what I'll 
probably do is something along the lines of,

     class MyFakeSubclass
     {
         MyBaseClass base;

         // ... extra data ...

         // ... manually-written wrappers
     }


More information about the Digitalmars-d-learn mailing list