What would be the consequence of implementing interfaces as fat pointers ?

Artur Skawina art.08.09 at gmail.com
Mon Mar 31 03:08:26 PDT 2014


On 03/31/14 05:32, Adam D. Ruppe wrote:
> On Monday, 31 March 2014 at 03:25:11 UTC, Manu wrote:
>> I also feel quite dirty using pointers in D where there is a dedicated reference type available. I don't want * and & to appear everywhere in my D code.
> 
> structs can pretty easily be reference types too:
> 
> struct RefType {
>    struct Impl {
>         // put all the stuff in here
>    }
>    Impl* impl;
>    alias impl this;
> 
>    // add ctors and stuff that new the impl
> }

No. This will bite you once you decide to overload Impl's ops, such
as indexing and slicing. And it's quite nasty, as the code will then
keep compiling but return bogus results. Debugging this will be an
"interesting" process, unless you're already aware of what's happening
or get lucky and the program crashes...

A little safer hack would be:

   struct RefType {
      struct Impl {
           // put all the stuff in here

           @disable this(this);
      }
      Impl* impl;
      ref Impl _get() @property { return *impl; }
      alias _get this;

      // add ctors and stuff that new the impl
   }

but this is still a rather ugly workaround.

artur


More information about the Digitalmars-d mailing list