foreach

Nick Treleaven via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 12 09:38:30 PDT 2014


On 12/06/2014 16:57, simendsjo wrote:
> On 06/12/2014 05:46 PM, monarch_dodra wrote:
>> On Thursday, 12 June 2014 at 15:09:51 UTC, simendsjo wrote:
>>>
>>> _ is an often used identifier for "i don't care" in many languages. The
>>> following works:
>>>    foreach(_; 0..n)
>>
>> One issue is that "_" is still an actual identifier, with normal name
>> collision rules. So that works only once. When you nest your loops,
>> you'll end up having conflicts, and name your "i don't care variable"
>> things such as "_", "__", "___", "_1", "_2" etc...
>>
>> It actually happens quite often I find.
>>
>
> Yeah, not good. Does any sane person use _ as a variable identifier and
> then reference it? A breaking change would be a special rule so _ can
> never be used and is allowed to shadow.

That might be a more flexible solution, because there is also this usage:

foreach (i, _; range){...}

I wonder if there may be other cases where '_' would be useful. 
Potentially, there's also tuple unpacking syntax:

auto (v, _) = myTuple;	// throw away second element

> Of course - this could break existing code, so it will never happen :)

I think we can break existing code if it's bad code. Using '_' as a 
variable name could be considered bad enough to disallow.


More information about the Digitalmars-d mailing list