Thread overview
[Issue 21243] Allow lambdas to return auto ref
Sep 18, 2022
Paul Backus
Sep 22, 2022
Dlang Bot
March 04, 2021
https://issues.dlang.org/show_bug.cgi?id=21243

Andrei Alexandrescu <andrei@erdani.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrei@erdani.com

--- Comment #1 from Andrei Alexandrescu <andrei@erdani.com> ---
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.

--
September 18, 2022
https://issues.dlang.org/show_bug.cgi?id=21243

--- Comment #2 from Paul Backus <snarwin+bugzilla@gmail.com> ---
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.

--
September 22, 2022
https://issues.dlang.org/show_bug.cgi?id=21243

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #3 from Dlang Bot <dlang-bot@dlang.rocks> ---
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

--