Make foreach element optional

Jack jckj33 at gmail.com
Tue Mar 16 16:51:02 UTC 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, _);



More information about the Digitalmars-d-learn mailing list