Bug 20238 – Add ability to specify ref argument for single-parameter lambdas without parentheses

Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-09-25T04:28:15Z
Last change time
2021-03-04T15:38:35Z
Keywords
spec
Assigned to
No Owner
Creator
Andrej Mitrovic

Comments

Comment #0 by andrej.mitrovich — 2019-09-25T04:28:15Z
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 by dfj1esp02 — 2019-09-25T08:22:21Z
Dunno, delegates already have quite overloaded and ambiguous syntax.
Comment #2 by b2.temp — 2019-11-03T12:52:47Z
*** Issue 20265 has been marked as a duplicate of this issue. ***
Comment #3 by andrei — 2021-03-04T15:38:35Z
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.