foreach/iota countdown

simendsjo simendsjo at gmail.com
Mon Feb 17 12:21:28 PST 2014


On Monday, 17 February 2014 at 20:03:32 UTC, Timon Gehr wrote:
> On 02/17/2014 08:33 PM, simendsjo wrote:
>> On Monday, 17 February 2014 at 19:30:38 UTC, Timon Gehr wrote:
>>> On 02/17/2014 08:22 PM, simendsjo wrote:
>>>> Should the following two uses be a compile-time error?
>>>>  foreach(i; 10 .. 0) // Never executes
>>>>
>>>>  foreach(i; iota(10, 0)) // .. neither does this
>>>>
>>>> I would like the second to either be a compile-time error or
>>>> automagically use a negative step.
>>>>
>>>> So we need to use a negative step in iota() or use a for loop
>>>>  foreach(i; iota(10, 0, -1)) // as expected
>>>
>>> The parameters can be runtime values. Auto-magically using a 
>>> negative
>>> step would hence be a bad idea.
>>
>> Why would it be a bad idea?
>
> The step direction shouldn't randomly change.

I wouldn't call it randomly. In that case you should call it 
randomly that it suddenly doesn't run once you try to step 
downward.

>> And I don't see where the runtime aspect comes in.
>
> It was just to illustrate the issue more clearly. Eg. it means 
> one and the same iota expression can sometimes iterate in one 
> direction and in the other direction at other times. That's 
> simply not useful behaviour.

I'm not sure what I find more confusing. Ok if it's a deliberate 
choice never to change the step direction, but is it deliberate 
that it's not an error to have a greater lower bound than upper 
bound? Or is this just the way it happened to be implemented?


More information about the Digitalmars-d-learn mailing list