foreach

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 12 20:29:33 PDT 2014


On Thu, 12 Jun 2014 23:04:08 -0400, Daniel Murphy  
<yebbliesnospam at gmail.com> wrote:

> "Manu via Digitalmars-d"  wrote in message
>> and
>> personally, I would expect an 'unreferenced variable' warning for the
>> unused loop counter. I like warnings hassling me about unused
>> variables.
>
> This is a good point.

In this case, it's being "used" but not by the user. The comparison and  
increment are done behind the scenes. Note that unused variables typically  
are on function parameters and are silenced by naming the type but not the  
variable. This request is not in line with that, it is asking for  
elimination of the variable and the type, in one special case.

If it were to be accepted, I'd push for foreach(x) instead of  
foreach(;0..x). Cut down all the noise, not just some of it.

>> I also object to the inconsistency with for(;;). Recall Scott Myers  
>> talk...
>
> Should we also allow "foreach(;)" ?  'for' being so loose is not  
> necessarily something we want to copy.

Completely different. foreach makes no sense without the second statement.

>
>> It's theoretically an optimisation too; capturing front may be a
>> costly operation that's not required. Ranges maintain their counters
>> internally, there's no reason to emit code to capture a local copy of
>> 'front' if it's not used. popFront and empty don't imply a byVal copy,
>> they usually just update range counters.
>
> The compiler's optimizer will do that just fine.

foreach over a range doesn't make any sense unless you are using the data.  
This is a non-issue IMO.

-Steve


More information about the Digitalmars-d mailing list