Parameter storage classes on foreach variables

Paul Backus snarwin at gmail.com
Tue May 21 13:51:17 UTC 2024


On Friday, 17 May 2024 at 18:59:13 UTC, Quirin Schroll wrote:
> ```d
> foreach (out T x; xs) { … }
> // lowers to
> {
>     auto __xs = xs; // or xs[]
>     for (; !__xs.empty /* or __xs.length > 0 or nothing */;)
>     {
>         auto x = T.init;
>>         __xs.put(x); /* or similar */
>     }
> }
> ```
[...]
> ```d
> int[string] aa;
> foreach (out key, out value; aa) { … }
> // lowers to
> {
>     auto __aa = aa;
>     for (;;)
>     {
>         KeyType key = KeyType.init;
>         ValueType value = ValueType.init;
>>         __aa[key] = value;
>     }
> }
> ```

I don't like these special-case rewrites. Binding an 
array/AA/range element to an `out` loop variable should have 
*exactly* the same semantics as binding a function argument to an 
`out` parameter. That is,

* The element must be an lvalue.
* The element is bound by reference.
* Upon being bound, the element is set its `.init` value.

So, no implicit calls to `put`, no implicit insertion of AA 
elements, etc.

Aside from that, this seems like a good idea to me. 👍


More information about the dip.ideas mailing list