[Issue 20238] New: Add ability to specify ref argument for single-parameter lambdas without parentheses
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Sep 25 04:28:15 UTC 2019
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 at puremagic.com
Reporter: andrej.mitrovich at 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.
--
More information about the Digitalmars-d-bugs
mailing list