www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Alias this property for getter and setter?

reply sighoya <sighoya gmail.com> writes:
I would like to alias this with properties in both directions 
such that this is not only destructured to the other value but 
also the other value is destructured to this.

My intention is to allow something like the following:

struct Ref(T)
{
     T* ptr;
     this(T* ptr)
     {
         this.ptr=ptr;
     }
      property T convert()
     {
         return *ptr;
     }

      property Ref!T convert(ref T t)
     {
         return Ref!T(&t);
     }
     alias convert this;
}


T sum(T)(SList!(Ref!T) list) if(T is Number)
{
    T sum=0;
    foreach(T elem; list)
    {
        sum+=elem;
    }
    return sum;
}
void testRef()
{
     int i=2;
     Ref!int reference=i;
     int j=reference;
}
Mar 21 2019
parent sighoya <sighoya gmail.com> writes:
The example presented tries to overcome the problem discovered in 
https://forum.dlang.org/post/xocnoxhuzoqqujwkgdtn forum.dlang.org.
Mar 21 2019