www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Limit Scope Restriction? with dmd -dip25 -dip1000

reply apz28 <home home.com> writes:
 safe:

class Foo
{
 safe:

     ubyte[] bytes()
     {
         return new ubyte[1];
     }
}

ubyte[] getBytes()
{
     scope foo = new Foo();
     return foo.bytes(); // Error: scope variable foo assigned to 
non-scope parameter this calling Foo.bytes
}

void main()
{	
     auto v = getBytes();
}
Apr 03 2021
parent Dennis <dkorpel gmail.com> writes:
On Saturday, 3 April 2021 at 15:32:38 UTC, apz28 wrote:
     return foo.bytes(); // Error: scope variable foo assigned 
 to non-scope parameter this calling Foo.bytes
Add the `scope` storage class to your `bytes()` function to tell the compiler that the function won't escape any references to class members. ```D ubyte[] bytes() scope { return new ubyte[1]; } ``` If you want the compiler to infer this, make it return `auto` or make it a template function.
Apr 03 2021