foreach

Nick Sabalausky via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 12 12:21:22 PDT 2014


On 6/12/2014 2:33 PM, Ary Borenszweig wrote:
> On 6/12/14, 3:04 PM, Nick Sabalausky wrote:
>> On 6/12/2014 11:00 AM, Manu via Digitalmars-d wrote:
>>> I often find myself wanting to write this:
>>>    foreach(; 0..n) {}
>>> In the case that I just want to do something n times and I don't
>>> actually care about the loop counter, but this doesn't compile.
>>>
>>> You can do this:
>>>    for(;;) {}
>>>
>>> If 'for' lets you omit any of the loop terms, surely it makes sense
>>> that foreach would allow you to omit the first term as well?
>>> I see no need to declare a superfluous loop counter when it is unused.
>>>
>>
>> I can't imagine this has ever been a significant issue for anyone. But
>> that said, I certainly can't disagree with it, and wouldn't object to it.
>>
>
> In Ruby/Crystal you can do:
>
> n.times do
>    # code
> end
>
> In D you have to write:
>
> for(unused; 0..n) {
>    # code
> }
>
> Doesn't it bother you that your language requires more typing and
> defining dummy variables just for doing something N times?
>

No.

- It's a negligible amount of extra typing.

- I find typing to be the most trivial and least time-consuming aspect 
of programming.

- I rarely need to do that. Most of my "N times" loops exist *because* I 
want to use the index.

> Note, I'm just trying to point out that small improvements in the
> programmers life will be thanked a lot and more people will join your
> language.

I think you've misunderstood my previous post. I *agree* with Manu. I 
just don't think it's particularly significant.



More information about the Digitalmars-d mailing list