www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - General reference types

reply dsimcha <dsimcha yahoo.com> writes:
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?
Mar 02 2009
next sibling parent dsimcha <dsimcha yahoo.com> writes:
== 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?

Mar 02 2009
prev sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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
Mar 02 2009