www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Non-covariance and overrides

reply Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
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
     }
Sep 02 2013
parent reply Jacob Carlborg <doob me.com> writes:
On 2013-09-02 17:00, Joseph Rushton Wakeling wrote:

 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
      }
You can use a template mixin or alias this. Template mixin: template MyBaseClassMixin () { void foo () { } // all your methods here } class MyFakeSubclass { mixin MyBaseClassMixin; } http://dlang.org/template-mixin.html Or using alias this: class MyFakeSubclass { MyBaseClass base alias base this; } Every method not available in MyFakeSubclass will be forwarded to "base". http://dlang.org/class.html#AliasThis -- /Jacob Carlborg
Sep 02 2013
parent reply Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
On 03/09/13 08:56, Jacob Carlborg wrote:
 class MyFakeSubclass
 {
      MyBaseClass base
      alias base this;
 }

 Every method not available in MyFakeSubclass will be forwarded to "base".

 http://dlang.org/class.html#AliasThis
OK, but the challenge of that approach is that base has to be public, no? Otherwise, its methods will not be accessible outside the class (even if the alias is itself public). And that then allows problems inasmuch as the user could then do something like, auto foo = new MyFakeSubclass; foo.base.bar(); ... and so bypass the alias.
Sep 07 2013
parent Jacob Carlborg <doob me.com> writes:
On 2013-09-07 13:42, Joseph Rushton Wakeling wrote:
 On 03/09/13 08:56, Jacob Carlborg wrote:
 class MyFakeSubclass
 {
      MyBaseClass base
      alias base this;
 }

 Every method not available in MyFakeSubclass will be forwarded to "base".

 http://dlang.org/class.html#AliasThis
OK, but the challenge of that approach is that base has to be public, no? Otherwise, its methods will not be accessible outside the class (even if the alias is itself public). And that then allows problems inasmuch as the user could then do something like, auto foo = new MyFakeSubclass; foo.base.bar(); ... and so bypass the alias.
I haven't checked if "base" can be private or not. But if that doesn't work try opDispatch: http://dlang.org/operatoroverloading.html#Dispatch You have a public opDispatch and a private "base". -- /Jacob Carlborg
Sep 08 2013