www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - reference variables don't exist, but can simulate them

reply NonNull <non-null use.startmail.com> writes:
Using gdc (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0
Please criticize:

struct refer(T) {
   T* ptr;
   this(ref T x) { ptr = &x; }
   ref T _() { return *ptr; }
   alias _ this;
   string toString() { import std.conv; return to!string(*ptr);  }
}

This will make a reference variable (simulation). [ toString() is 
just for writeln. ]

void main() {
     int i = 100;
     refer!int j = i;
     j = 3;
     writeln(i);
     i = 100;
     writeln(j);
     j += 3;
     writeln(i);
     refer!int k = j;
     writeln(k);
}

And a refer!int can be returned as it is just a value. Returning 
one that contains a pointer to a local variable leads to a 
compilation error.
Jun 28 2020
parent reply NonNull <non-null use.startmail.com> writes:
On Sunday, 28 June 2020 at 20:59:59 UTC, NonNull wrote:
 Using gdc (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0
 Please criticize:

 struct refer(T) {
   T* ptr;
   this(ref T x) { ptr = &x; }
   ref T _() { return *ptr; }
   alias _ this;
   string toString() { import std.conv; return to!string(*ptr);  
 }
 }

 This will make a reference variable (simulation). [ toString() 
 is just for writeln. ]

 void main() {
     int i = 100;
     refer!int j = i;
     j = 3;
     writeln(i);
     i = 100;
     writeln(j);
     j += 3;
     writeln(i);
     refer!int k = j;
     writeln(k);
 }

 And a refer!int can be returned as it is just a value. 
 Returning one that contains a pointer to a local variable leads 
 to a compilation error.
* does not lead to a compilation error
Jun 28 2020
parent reply NonNull <non-null use.startmail.com> writes:
On Sunday, 28 June 2020 at 21:01:36 UTC, NonNull wrote:
 On Sunday, 28 June 2020 at 20:59:59 UTC, NonNull wrote:
 Using gdc (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0
 Please criticize:

 struct refer(T) {
   T* ptr;
   this(ref T x) { ptr = &x; }
   ref T _() { return *ptr; }
   alias _ this;
   string toString() { import std.conv; return to!string(*ptr);
  }
 }

 This will make a reference variable (simulation). [ toString() 
 is just for writeln. ]

 void main() {
     int i = 100;
     refer!int j = i;
     j = 3;
     writeln(i);
     i = 100;
     writeln(j);
     j += 3;
     writeln(i);
     refer!int k = j;
     writeln(k);
 }

 And a refer!int can be returned as it is just a value. 
 Returning one that contains a pointer to a local variable 
 leads to a compilation error.
* does not lead to a compilation error
Now with a different compiler I this: Deprecation: Cannot use alias this to partially initialize variable j of type refer. Use j._() This is for the line j=3 What is this about? Where does this hidden rule come from?
Jun 28 2020
parent Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Monday, 29 June 2020 at 02:11:15 UTC, NonNull wrote:

 Deprecation: Cannot use alias this to partially initialize 
 variable j of type refer. Use j._()

 This is for the line j=3

 What is this about? Where does this hidden rule come from?
That one comes from [1]. But there are quite a few more "hidden" rules that you're violating here. Try putting safe on your main and compiling with -preview=dip1000 (for dmd, refer to your compiler's help if you're using others). The bulk of escape analysis is only done for safe, and only with that DIP enabled (IIRC only some trivial checks are done otherwise). [1] https://issues.dlang.org/show_bug.cgi?id=19441
Jun 28 2020