View mode: basic / threaded / horizontal-split · Log in · Help
March 02, 2009
General reference types
I've been thinking that it would be nice for D to have a general reference
type, which would basically be syntactic sugar for a pointer with no
arithmetic allowed.  This would be safer and more syntactically elegant than
using pointers directly just to get reference semantics for something.  Modulo
a few small rough edges in D's operator overloading, this can be done well in
a library, so core language support is unnecessary.

Such a type might something like:

struct Ref!(T) {
   private T* ptr;

   // Assign val to dereference of ptr.
   void opAssign(U)(ref U val) if(is(U : T));

   // Rebind ptr.
   void opAssign(Ref!(T) val);

   ref T opDot() {
       return *ptr;
   }

   static if(__traits(compiles, (*ptr)++)) {
       void opPostInc() {
           (*ptr)++;
       }
   }

   // Other stuff.
}

I've started prototyping this a little, and filing bug reports/enhancement
requests for a few small hangups I've run into implementing this.  Does this
sound worth doing?  If so, any input on how it should work?
March 02, 2009
Re: General reference types
== Quote from dsimcha (dsimcha@yahoo.com)'s article
> I've been thinking that it would be nice for D to have a general reference
> type, which would basically be syntactic sugar for a pointer with no
> arithmetic allowed.  This would be safer and more syntactically elegant than
> using pointers directly just to get reference semantics for something.  Modulo
> a few small rough edges in D's operator overloading, this can be done well in
> a library, so core language support is unnecessary.
> Such a type might something like:
> struct Ref!(T) {
>     private T* ptr;
>     // Assign val to dereference of ptr.
>     void opAssign(U)(ref U val) if(is(U : T));

Sorry, this method wouldn't exist.  Instead it would be:

// Make this a reference to val.
void opAssign(ref T val);

>     // Rebind ptr.
>     void opAssign(Ref!(T) val);
>     ref T opDot() {
>         return *ptr;
>     }
>     static if(__traits(compiles, (*ptr)++)) {
>         void opPostInc() {
>             (*ptr)++;
>         }
>     }
>     // Other stuff.
> }
> I've started prototyping this a little, and filing bug reports/enhancement
> requests for a few small hangups I've run into implementing this.  Does this
> sound worth doing?  If so, any input on how it should work?
March 02, 2009
Re: General reference types
dsimcha wrote:
> I've been thinking that it would be nice for D to have a general reference
> type, which would basically be syntactic sugar for a pointer with no
> arithmetic allowed.  This would be safer and more syntactically elegant than
> using pointers directly just to get reference semantics for something.  Modulo
> a few small rough edges in D's operator overloading, this can be done well in
> a library, so core language support is unnecessary.
> 
> Such a type might something like:
> 
> struct Ref!(T) {
>     private T* ptr;
> 
>     // Assign val to dereference of ptr.
>     void opAssign(U)(ref U val) if(is(U : T));
> 
>     // Rebind ptr.
>     void opAssign(Ref!(T) val);
> 
>     ref T opDot() {
>         return *ptr;
>     }
> 
>     static if(__traits(compiles, (*ptr)++)) {
>         void opPostInc() {
>             (*ptr)++;
>         }
>     }
> 
>     // Other stuff.
> }
> 
> I've started prototyping this a little, and filing bug reports/enhancement
> requests for a few small hangups I've run into implementing this.  Does this
> sound worth doing?  If so, any input on how it should work?

Mosey to your copy of std.typecons and you'll see the (not yet 
documented) struct Ref.

Andrei
Top | Discussion index | About this forum | D home