www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20238] New: Add ability to specify ref argument for

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

          Issue ID: 20238
           Summary: Add ability to specify ref argument for
                    single-parameter lambdas without parentheses
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: andrej.mitrovich gmail.com

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.

--
Sep 24 2019