On Wednesday, 29 August 2012 at 01:42:36 UTC, Timon Gehr wrote:
Not exactly the same thing (what you propose would have different
IFTI behaviour), but works quite well:

import std.stdio;

struct Ref(T){
    private T* _payload;
    this(ref T i){_payload = &i; }
    @property ref T deref(){ return *_payload; }
    alias deref this;
}
auto ref_(T)(ref T arg){return Ref!T(arg);}

void main(){
    int i,j;
    auto r = i.ref_;
    r++;
    auto q = r;
    writeln(r," ",q);
    q++;
    writeln(r," ",q);
    q = j.ref_;
    q++;
    writeln(r," ",q.deref);
}

I did figure that that's possible. But, to me, having reference variables be implemented in a library instead of them being a core language feature, is like having pointers implemented as a library. I'd like to have a good answer when some newcomer asks me: "why, oh why is this so?".

Reply via email to