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

Steven Schveighoffer schveiguy at
Mon May 18 09:21:10 PDT 2009

On Mon, 18 May 2009 12:02:30 -0400, Andrei Alexandrescu  
<SeeWebsiteForEmail at> wrote:

> Steven Schveighoffer wrote:
>> On Sun, 17 May 2009 20:28:24 -0400, Andrei Alexandrescu  
>> <SeeWebsiteForEmail at> 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?

hm... I guess that's one way to look at it.  I don't anticipate many  
typing this by mistake.  One thing that would be left to decide, 1..4  
means 1-2-3, should 4..1 mean 4-3-2 or 3-2-1?  I'd vote for 4-3-2 to be  
consistent on having x..y mean inclusive to exclusive.

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

As I said later, I'm not stuck on the name.  Just that it should be a  
property (BTW, has the requirement for parens after an array "property"  
been fixed yet?).

I don't care if it's a library solution as long as the library solution  
can perform as well as the current solution, and can implement the api of  
the current solution (i.e. can also iterate indexes).  Otherwise, you'll  
have grumbling ;)


More information about the Digitalmars-d mailing list