Unpacking syntax
Timon Gehr
timon.gehr at gmx.ch
Fri Sep 6 07:35:35 UTC 2024
On 9/6/24 00:08, Timon Gehr wrote:
> These are my ideas for how to support unpacking any tuple-like type into
> multiple components.
>
> ...
Jacob asked on Discord whether in a `foreach` statement, key and value
can both be unpacked, and whether in a foreach statement, `(const x, y)`
is also allowed. Both of those work. In particular, this compiles and
runs with my `unpacking` branch of DMD:
```d
import std.typecons: t=tuple, T=Tuple;
void main(){
auto aa=[t(1, 2): t(3, 4), t(5, 6): t(7, 8)];
foreach((const k0, k1), (v0, ref v1); aa){
k1 = k0;
v1 = v0;
v0 = 22;
}
assert(aa == [t(1, 2): t(3, 3), t(5, 6): t(7, 7)]);
foreach((const x, y); [t(1, 2), t(2, 3)]){}
}
```
More information about the dip.ideas
mailing list