Bug 21243 – Allow lambdas to return auto ref

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2020-09-13T15:50:50Z
Last change time
2022-09-22T18:27:34Z
Assigned to
No Owner
Creator
Paul Backus

Comments

Comment #0 by snarwin+bugzilla — 2020-09-13T15:50:50Z
Nested functions (to which lambdas are lowered) already support this, so lambdas should too. This will make it possible to, for example, enclose an arbitrary expression in a @trusted lambda without needing to check first whether the expression evaluates to an lvalue or an rvalue.
Comment #1 by andrei — 2021-03-04T16:32:58Z
Example per Paul: void main() { alias f1(alias x) = () => x; int n = 123; assert(__traits(compiles, f!123())); // ok assert(__traits(compiles, f!n() = 456)); // no good } If we add ref to the return value of the lambda, the other assertion fails.
Comment #2 by snarwin+bugzilla — 2022-09-18T21:20:41Z
There is another example use-case in issue 16034. Currently, `isInputRange` always evaluates to `false`, for ranges with non-copyable elements, because it checks for the presence of `.front` by attempting to compile the following lambda: (R r) => r.front If `r.front` returns by `ref`, this will cause its return value to be copied, which in turn causes compilation to fail when its type is non-copyable. With `auto ref` lambdas, this could be fixed by changing the lambda to auto ref (R r) => r.front As-is, the fix will require a more invasive change.
Comment #3 by dlang-bot — 2022-09-22T18:27:34Z
dlang/dmd pull request #14454 "Fix issue 21243 - Allow lambdas to return auto ref" was merged into master: - fac01cc29140a15f2f17cd146dc8bf507e4f57fe by Paul Backus: Fix issue 21243 - Allow lambdas to return auto ref https://github.com/dlang/dmd/pull/14454