D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20238 - Add ability to specify ref argument for single-parameter lambdas without parentheses
Summary: Add ability to specify ref argument for single-parameter lambdas without pare...
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords: spec
Depends on:
Blocks:
 
Reported: 2019-09-25 04:28 UTC by Andrej Mitrovic
Modified: 2021-03-04 15:38 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2019-09-25 04:28:15 UTC
When using 'each', you have to be careful not to use pass-by-value:

-----
import std.algorithm;

void main()
{
    struct S { int x; }

    auto arr = [S(0), S(0), S(0), S(0)];
    arr.each!(elem => elem.x = 1);  // no-op, it was pass by value
    assert(arr != [S(1), S(1), S(1), S(1)]);

    arr.each!((ref elem) => elem.x = 1);  // ok, this works
    assert(arr == [S(1), S(1), S(1), S(1)]);

    arr.each!(ref elem => elem.x = 1);  // why not this syntax too?
    assert(arr == [S(1), S(1), S(1), S(1)]);
}
-----

It would be really cool if we could use the proposed syntax in the third call.
Comment 1 anonymous4 2019-09-25 08:22:21 UTC
Dunno, delegates already have quite overloaded and ambiguous syntax.
Comment 2 basile-z 2019-11-03 12:52:47 UTC
*** Issue 20265 has been marked as a duplicate of this issue. ***
Comment 3 Andrei Alexandrescu 2021-03-04 15:38:35 UTC
In the notation

    ref elem => elem.x = 1

it is unclear whether `ref` refers to the return value of the lambda or the parameter. These do work:

    (ref elem) => elem.x = 1
    ref (elem) => elem.x = 1

(The second would be rejected because it returns a reference to a local.)

I'll close this, please reopen if appropriate.