www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Bug with multiple subtyping?

reply Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
Hello all,

TDPL gives a description in Section 6.13 (pp. 230-233) of a method for
subtyping 
by storing a private instance of the base type and using "alias ... this" to 
access its methods.  The example given is as follows:

class StorableShape : Shape
{
     private DBObject _store;
     alias _store this;

     this()
     {
         _store = new DBObject;
     }
}

However, I've found that this won't work because the "private" keyword means 
that functions and objects outside the module won't be able to access the
public 
methods of _store, even though the alias itself is public.

I've attached a small example with 2 classes, A and B, where B stores an 
internal copy of A in a similar manner.  In this case, if you run rdmd 
inherit.d, you'll get an error:

   inherit.d(8): Error: class inheritance.B member base is not accessible

Is this a bug, or intentional, and if it's intentional, how can one make the 
public methods of the "base" class accessible while keeping the instance
private?

Thanks & best wishes,

     -- Joe
Sep 07 2013
parent reply Jacob Carlborg <doob me.com> writes:
On 2013-09-07 16:10, Joseph Rushton Wakeling wrote:
 Hello all,

 TDPL gives a description in Section 6.13 (pp. 230-233) of a method for
 subtyping by storing a private instance of the base type and using
 "alias ... this" to access its methods.  The example given is as follows:

 class StorableShape : Shape
 {
      private DBObject _store;
      alias _store this;

      this()
      {
          _store = new DBObject;
      }
 }

 However, I've found that this won't work because the "private" keyword
 means that functions and objects outside the module won't be able to
 access the public methods of _store, even though the alias itself is
 public.

 I've attached a small example with 2 classes, A and B, where B stores an
 internal copy of A in a similar manner.  In this case, if you run rdmd
 inherit.d, you'll get an error:

    inherit.d(8): Error: class inheritance.B member base is not accessible

 Is this a bug, or intentional, and if it's intentional, how can one make
 the public methods of the "base" class accessible while keeping the
 instance private?

 Thanks & best wishes,

      -- Joe
Try using opDispatch as a described in the other thread. -- /Jacob Carlborg
Sep 08 2013
parent Joseph Rushton Wakeling <joseph.wakeling webdrake.net> writes:
On 08/09/13 21:44, Jacob Carlborg wrote:
 Try using opDispatch as a described in the other thread.
I'll give that a go, but I still think this is a bug that needs fixing. The example in TDPL p. 231 will very clearly also not work due to this issue.
Sep 08 2013