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

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon May 18 09:02:30 PDT 2009


Steven Schveighoffer wrote:
> On Sun, 17 May 2009 20: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.
> 
> foreach_reverse sucks in its own right, while you're looking for stuff 
> to get rid of, I think it can be done better.
> 
> foreach(x; 4 .. 1 ) should do a reverse interval (and looks way more 
> readable).

I'm a bit leery about this - what if user code has a bug and I transform 
that into a feature?

> foreach(x; array.reverse) should iterate in reverse.

foreach (x; array.retro) already works.

> for classes, using a method called reverse should work fine:
> 
> foreach(x; myclass.reverse)
> 
> Having to implement opApplyReverse is rediculous, and having a keyword 
> like foreach_reverse is rediculous.  Reverse isn't the only interesting 
> iteration pattern, so why focus a whole keyword and syntax on that?

I agree.


Andrei



More information about the Digitalmars-d mailing list