foreach (x; a .. b) and foreach_reverse (x; a .. b) should be disallowed for floats

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon May 18 08:54:53 PDT 2009


Denis Koroskin wrote:
> On Mon, 18 May 2009 04:28:24 +0400, Andrei Alexandrescu 
> <SeeWebsiteForEmail at erdani.org> wrote:
> 
>> Consider:
>>
>>      foreach (x; 1.0 .. 4.1) writeln(x);
>>      foreach_reverse (x; 1.0 .. 4.1) writeln(x);
>>
>> This sucks. foreach with interval for floating-point types should be 
>> disallowed.
>>
>>
>> Andrei
> 
> It's useless, unless a step is specified (btw, integer iteration would 
> benefit from having a step, too)
> 
> I don't mind if the whole .. feature is removed. It could be implemented 
> in a library, with an optional step:
> 
> foreach (x; range(0, 100)) { // step is 1 implicitly
>    // ...
> }
> 
> foreach (x; range(0, 100, 2)) { // explicit step
>    // ...
> }
> 
> This feels more functional, although slightly longer to type.
> 

It's actually there already: std.range.iota. Incidentally it does the 
right thing with floats because it iterates by the recurrence formula 
iter(i) = lowest + i * step (where i is an integer). But there's an 
unrelated bugzilla about it.


Andrei



More information about the Digitalmars-d mailing list