Enhancing foreach

ixid nuaccount at gmail.com
Wed Jan 9 09:05:23 PST 2013


On Wednesday, 9 January 2013 at 08:01:05 UTC, Jacob Carlborg 
wrote:
> On 2013-01-09 05:38, ixid wrote:
>> A very minor change that would be elegant and easy for 
>> beginners:
>>
>> foreach(i;5)
>>     //stuff
>>
>> Allowing just a single number to mean the end point and a 
>> default
>> starting point of zero is assumed, just as with iota it's 
>> possible to
>> write it iota(5) or even 5.iota, it assumes unless otherwise 
>> specified
>> that you mean 0 to be the starting point. Would this be a 
>> reasonable
>> enhancement request for me to make or does it collide with 
>> something?
>
> I don't see what this would gain over:
>
> foreach (i ; 0 .. 5)

Just like the terser iota, it's more elegant and easier for newer 
users. The fewer symbols there are in code the easier it is to 
parse for the user (up to a point). It's also more consistent 
with iota. It also means that a foreach that iterates a function 
return value can be written in the same manner whether it returns 
an array or an integer, again more consistency.


More information about the Digitalmars-d-learn mailing list