Proposal: Reference Variables

Bruno Medeiros brunodomedeirosATgmail at SPAM.com
Sun Apr 9 11:36:05 PDT 2006


S. Chancellor wrote:
> I would like to suggest the following variable type added to D as a 
> supplement to pointers.  Please review the following code and suggest 
> modifications and additions as you see.
> 
> Thanks!
> 
> -S.
> 
> Oh yes, and please assume all the following asserts PASS.
> 
> 
> long x = 2;
> long& y = &x;
> 
> assert(y == 2);
> 
> y = 4;
> 
> assert(x == 4);
> 
> assert(&y == &x);  //Notice the change in the operation of &.  Not a 
> handle to x!
> 
> long z = 5;
> 
> &y = &z;  //& Allows l-value assignment,
> 
> assert( y == 5 );
> 
> assert( &y == &z );
> 
> assert(y.sizeof == z.sizeof);  //Sizeof gives information on referenced 
> type, not reference.  Ditto for other properties
> 

I would moderately prefer a syntax with "ref" instead of "&", as:
   long x = 1;
   ref long z = &x;
Also, "ref" (or "&") would supersede the functionality of "inout", since 
they do the same, only that "inout" only works for parameter variables.

-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list