April 03, 2021

@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();
}

April 03, 2021

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.

     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.