[Issue 4112] New: Stride in foreach ranges

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Apr 21 14:09:49 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4112

           Summary: Stride in foreach ranges
           Product: D
           Version: future
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-04-21 14:09:48 PDT ---
In D2 the range syntax of foreach is useful because it helps state the
iteration variable one time only instead of tree, it's cleaner and less
bug-prone than the normal for syntax, and it's useful in many situations
because many for loops are just over a numeric range (so the normal for syntax
can be left for more complex situations).

But beside natural series, for loops over arithmetic ranges too are common.
Currently in D2 this requires a normal for syntax:

for (int i = 0; i < 100; i += 2) {}

My experience with Python tells me that an optional stride can be useful here.
This is a possible syntax (the colon is useful to visually tell apart the third
value):

foreach (i; 0 .. 100 : 2) {}

(To keep things simpler, it can even be acceptable for the stride value to be a
value known at compile-time).

See also bug 4111.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list