On Wednesday, 24 July 2019 at 12:54:51 UTC, aliak wrote:
Trying to get dip1000 flag in use. I have this error:

Error: returning Optional(null, false).this(value) escapes a reference to parameter value, perhaps annotate with return

in this function:

public auto some(T)(auto ref T value) {
    return Optional!T(value); // <-- error on this line
}

And it's instantiated from here:

static Optional!T fromRepresentation(Json value) {
  if (value == Json.undefined) {
    return no!T;
  }
return some(deserializeJson!T(value)); // <-- instantiated from here
}

I put the qualifier "return" on the parameter to some, but did nothing. And I put it on Optional.this() as well, but I'm not sure where I'm supposed to put it.

It should go on the constructor's parameter; i.e.,

this(auto return ref T value) { /* ... */ }

Under the hood, a constructor actually returns the constructed value by reference, so the actual signature of the above constructor seen by the lifetime checker is:

ref Optional!T __ctor(auto return ref T value)

You can see this for yourself with something like `pragma(msg, typeof(Optional!T.__ctor))`.

Reply via email to