Wouldn't the compiler be smart with this shadowing variable?

Paul Backus snarwin at gmail.com
Sat Dec 11 21:35:41 UTC 2021


On Saturday, 11 December 2021 at 20:22:13 UTC, frame wrote:
> ```d
> // iteration works, but scope behind does nothing
> for({int i=0; j=0;} i<10; ++i, {++i; ++j;} ){}
>
> // endless loop
> for({int i=0; j=0;} i<10; {++i; ++j;} ){}
> ```
>
> Not sure if bug or feature but it is inconsistent for me and 
> thus a reason to avoid it.

That's because `{++i; ++j;}` in an expression context is 
shorthand syntax for `delegate void () {++i; ++j;}`.

If you look at the [grammar for the `for` statement][1], you'll 
see that the "initialize" part allows a *statement*, but the 
"test" and "increment" parts only allow *expressions*.

[1]: https://dlang.org/spec/statement.html#ForStatement


More information about the Digitalmars-d-learn mailing list