foreach

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 12 21:14:08 PDT 2014


On Thu, 12 Jun 2014 23:49:46 -0400, Manu via Digitalmars-d  
<digitalmars-d at puremagic.com> wrote:

> On 13 June 2014 13:29, Steven Schveighoffer via Digitalmars-d
> <digitalmars-d at puremagic.com> wrote:
>> 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 compiler doesn't use the result of front().

foreach(x; 0..5) translates to for(auto x = 0; x < 5; ++x)

So there is no "front"

>
>
>> The comparison and increment are done behind the scenes.
>
> These use popFront() and empty(), not front().

I don't understand, 0..n is not a range.

>
>
>> 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.
>
> How is it a special case? It would be consistent with for(;;)

foreach(x; 0..5) is a special case of foreach.

>> 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.
>
> Fine with me. I'd prefer this too.
>
>
>> foreach over a range doesn't make any sense unless you are using the  
>> data.
>> This is a non-issue IMO.
>
> Who says?
> You can't possibly conceive of a case where the length is the only
> interesting property? (obviously I'm confronted by the case now, and
> not for the first time)

Then use length or walkLength

> You'd argue for foreach(_; 0..myRange.length)? That's pretty awkward.

It's how I would do it, but I'd use 'i' instead of '_'

> Some forward ranges don't have a known length, and can only be summed
> by an iteration sweep.

http://dlang.org/phobos/std_range.html#.walkLength

-Steve


More information about the Digitalmars-d mailing list