Thread overview
Make foreach element optional
Mar 16, 2021
Per Nordlöw
Mar 16, 2021
Imperatorn
Mar 16, 2021
Per Nordlöw
Mar 16, 2021
Imperatorn
Mar 16, 2021
bachmeier
Mar 16, 2021
Jack
Mar 16, 2021
bachmeier
March 16, 2021
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
March 16, 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

foreach(0..n) could work. Why though.
March 16, 2021
On Tuesday, 16 March 2021 at 13:31:34 UTC, Imperatorn wrote:
> foreach(0..n) could work. Why though.

When performing a side-effect n times.
March 16, 2021
On 3/16/21 8:49 AM, 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

Meh, is this a common need though? The first form isn't terrible.

In general, I'd say it would be nice to designate _ as an unused variable (i.e. not allowed to access it, and it doesn't trigger shadowing errors). It's like this in Swift for instance.

-Steve
March 16, 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).
March 16, 2021
On Tuesday, 16 March 2021 at 13:52:29 UTC, Per Nordlöw wrote:
> On Tuesday, 16 March 2021 at 13:31:34 UTC, Imperatorn wrote:
>> foreach(0..n) could work. Why though.
>
> When performing a side-effect n times.

Then why not just do:

auto times(alias F, T)(T number)
{
    return number.iota.each!(_ => F());
}

void f()
{
    writeln("hi");
}

n.times!(f);

?
March 16, 2021
On Tuesday, 16 March 2021 at 15:02:54 UTC, Steven Schveighoffer wrote:
> On 3/16/21 8:49 AM, 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
>
> Meh, is this a common need though? The first form isn't terrible.
>
> In general, I'd say it would be nice to designate _ as an unused variable (i.e. not allowed to access it, and it doesn't trigger shadowing errors). It's like this in Swift for instance.
>
> -Steve

the _ as unused variable would be useful when the parameter has out parameter but wouldn't to ignore it. C# does something like this currently.

int foo(int x, out bool state) { }

// only wants return value
int y = foo(x, _);

March 16, 2021
On Tuesday, 16 March 2021 at 16:29:45 UTC, Imperatorn wrote:
> On Tuesday, 16 March 2021 at 13:52:29 UTC, Per Nordlöw wrote:
>> On Tuesday, 16 March 2021 at 13:31:34 UTC, Imperatorn wrote:
>>> foreach(0..n) could work. Why though.
>>
>> When performing a side-effect n times.
>
> Then why not just do:
>
> auto times(alias F, T)(T number)
> {
>     return number.iota.each!(_ => F());
> }
>
> void f()
> {
>     writeln("hi");
> }
>
> n.times!(f);
>
> ?

To my knowledge, there's nothing like this in the standard library or the language. I used something similar but eventually decided it was simpler to use the original foreach. It'd honestly have to be a language change to be useful. (Given the benefit, I doubt this would happen.)