digitalmars.D.bugs - [Issue 23352] New: `in` and `out` foreach variables
- d-bugmail puremagic.com (30/32) Sep 21 2022 https://issues.dlang.org/show_bug.cgi?id=23352
https://issues.dlang.org/show_bug.cgi?id=23352 Issue ID: 23352 Summary: `in` and `out` foreach variables Product: D Version: D2 Hardware: All OS: All Status: NEW Severity: enhancement Priority: P1 Component: dmd Assignee: nobody puremagic.com Reporter: qs.il.paperinik gmail.com In Herb Sutter’s proposal for a brand new C++ syntax¹, I found this gem:“In this proposal, for(in auto x : rng) automatically is both efficient and guaranteed to be read-only.”This is something D could do, too. Our `foreach` loops can be foreach ( element; range) // copy foreach ( ref element; range) // reference² foreach (auto ref element; range) // copy/reference, depending on value category foreach ( in element; range) // see next paragraph foreach ( out element; range) // see next paragraph `in` and `out` elements have the same binding as `in` and `out` function parameters. They bind to opApply(int delegate(in Type)); and opApply(int delegate(out Type)); respectively. ¹ https://raw.githubusercontent.com/hsutter/708/main/708.pdf ² That `ref` is effectively `auto ref` is a known bug. --
Sep 21 2022