Thread overview
Bug with multiple subtyping?
Sep 08, 2013
Jacob Carlborg
September 07, 2013
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


September 08, 2013
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
September 08, 2013
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.