Can we fix this?

jfondren julian.fondren at gmail.com
Wed Sep 29 17:00:41 UTC 2021


On Wednesday, 29 September 2021 at 16:47:23 UTC, Steven 
Schveighoffer wrote:
> ```d
> foreach(int i; [1, 2, 3]) {
>    dgList ~= (b) { return {writeln(b);};} (i + 2);
>    // or less error prone:
>    dgList ~= (i) { return {writeln(i + 2);};} (i);
> }
>
> ```
>
> Which specifies the captured variable. But it's a lot of 
> punctuation.

```
dgList ~= (i => { writeln(i + 2); })(i);
```

is cleaner, but ISRT that this usage will be banned because 
people rarely intend what it actually means.

I thought I tried this exact workaround and it didn't work, so I 
guessed that the optimizer had deleted the extra closure. But 
that does work, there.

>
> Some imagined syntax?
>
> ```d
> dgList ~= (@capture i) {writeln(i + 2);};
> // or:
> dgList ~= (@capture i) => writeln(i + 2);
> ```
>
> -Steve

Or a separate param list, to be similar to template params? 
`(i)() { writeln(i + 2); };`

Explicit captures are at least easy to explain since other 
languages have already gone this route.


More information about the Digitalmars-d mailing list