return attribute with latest DMD - dmd-nightly -unittest -dip1000

apz28 home at home.com
Thu Nov 3 02:56:33 UTC 2022


why foo2 is not inferred as scope which is shorter scope then 
foo1?

import std.stdio;

     @safe:

     class Obj {@safe: override string toString() { return "obj"; 
} }

     struct Foo1
     {@safe:
         this(Obj obj) { this._obj = obj; }
         ~this() { _obj = null; } // do something with obj
         // _obj must not be out live with Foo1, so need return 
attribute
         @property Obj obj() return { return _obj; }
         private Obj _obj;
     }

     struct Foo2
     {@safe:
         this(Obj obj) { this._obj = obj; }
         ~this() { _obj = null; } // do something with obj
         void call() { writeln(_obj.toString()); }
         // _obj must not be out live with Foo1, so need return 
attribute
         @property Obj obj() return { return _obj; }
         private Obj _obj;
     }

     void call1() { auto obj = new Obj(); auto foo1 = Foo1(obj); 
call2(foo1); }

// foo2 borrows foo1.obj and do something with it
void call2(ref Foo1 foo1) { auto foo2 = Foo2(foo1.obj); 
call3(foo2); }

void call3(ref Foo2 foo2) { foo2.call(); }

void main() { call1(); }


Getting below error message
onlineapp.d(29): Error: reference to local variable `foo1` 
assigned to non-scope parameter `obj` calling onlineapp.Foo2.this

Remove "return" from property obj fix the error message




More information about the Digitalmars-d-learn mailing list