Make foreach element optional
bachmeier
no at spam.net
Tue Mar 16 15:34:27 UTC 2021
On Tuesday, 16 March 2021 at 12:49:13 UTC, Per Nordlöw wrote:
> I find myself writing
>
> foreach (_; 0 .. n)
> doSomething(); // no using the variable `_`
>
> .
>
> What about relaxing the syntax to allow
>
> foreach (; 0 .. n)
>
> and/or
>
> foreach (0 .. n)
>
> ?
>
> Thereby making the `ForeachTypeList` of `AggregateForeach` in
> the grammar rule [1] optional.
>
> [1] https://dlang.org/spec/statement.html#foreach-statement
The gain to altering the foreach statement is minimal since _ is
a nice convention to use if you don't need the value of the
counter.
Something like this gives cleaner code:
replicate(100) {
// do stuff with side effects
}
I don't know if it would be an opportunity for a compiler
optimization (probably not).
More information about the Digitalmars-d-learn
mailing list