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

dsimcha dsimcha at yahoo.com
Sun May 17 18:17:03 PDT 2009


== Quote from Andrei Alexandrescu (SeeWebsiteForEmail at erdani.org)'s article
> 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

This one I agree with.  I wasn't even aware that the syntax above worked.  The use
of 4.1 to deal with the fact that the interval is exclusive is a really silly
kludge caused by this feature not being intended to be used with floating point
intervals.  It would be an argument for making the foreach interval loop more
powerful instead, except that if you're going to do that, you may as well just use
a regular for loop, since foreach interval loops are just a shortcut for this anyhow.



More information about the Digitalmars-d mailing list