[Issue 16034] map should be possible with a reference only

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Sep 18 21:27:49 UTC 2022


https://issues.dlang.org/show_bug.cgi?id=16034

Paul Backus <snarwin+bugzilla at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |snarwin+bugzilla at gmail.com

--- Comment #3 from Paul Backus <snarwin+bugzilla at gmail.com> ---
The problem is not the lambda's parameter, but its return value.

Essentially, we would like the lambda to return by `ref` when `r.front` returns
by `ref`, so that it does not create an unnecessary copy. For a normal
function, the solution would be to use `auto ref`, but the lambda syntax does
not support this (issue 21243), so we must either fix that issue first or find
a workaround.

One workaround is to use a string mixin, like this:

private enum getFrontLambda = q{(return ref R r) => r.front};

enum bool isInputRange(R) =
    /* ... */
    && (
        is(typeof(mixin("ref ", getFrontLambda)))
        || is(typeof(mixin(getFrontLambda)))
    )
    /* ... */

This essentially replicates the logic of inferring `ref` in user code: attempt
to compile with `ref`, then fall back to non-`ref` if it fails.

--


More information about the Digitalmars-d-bugs mailing list