Why can't we make reference variables?
Tommi
tommitissari at hotmail.com
Tue Aug 28 19:04:50 PDT 2012
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?".
More information about the Digitalmars-d
mailing list