[Issue 23352] New: `in` and `out` foreach variables
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Sep 21 08:49:45 UTC 2022
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 at puremagic.com
Reporter: qs.il.paperinik at 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.
--
More information about the Digitalmars-d-bugs
mailing list