[Issue 4112] Stride in foreach ranges

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Feb 9 10:19:36 PST 2011


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



--- Comment #4 from bearophile_hugs at eml.cc 2011-02-09 10:17:09 PST ---
(In reply to comment #1)
> or better (see http://d.puremagic.com/issues/show_bug.cgi?id=5395)
>     foreach (n ; (1..1000).byStep(3) {...}
> 
> What do you think?

Your syntax is too much wordy for something so common as this. There are too
many parentheses too, that increase syntax noise and cause errors similar to
your one, you have forgotten a closing ).


> By the way, this issue applies to your proposal as well: what does 1..3:-2
> Actually mean?

In Python it gives an empty range. The semantics of a negative stride is well
founded in Python:

>>> range(1, 3, -2)
[]


> Finally, if ever we had a step syntax for i..j, I would prefere '/' than ':',
> read as "by": 
>     foreach (; i..j:k
> --> traverse the interval i..j 'by' (steps of) k.

The / syntax means something different already:

import std.stdio;
void main() {
    int i = 10;
    int j = 100;
    int k = 5;
    foreach (x; i .. j / k)
        writeln(x);
}

==>
10
11
12
13
14
15
16
17
18
19

-- 
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