bearophile wrote:
Russel Winder:
Perhaps this needs review. All modern language now have this as an
integral way of describing a sequence of values.
We have discussed about this not too much time ago. See the enhancement request:
http://d.puremagic.com/issues/show_bug.cgi?id=5395
D language design is too much un-orthogonal about this, and I'd like to see
this situation improved.
Currently there are several separated syntaxes and means to specify an interval:
1) foreach interval syntax, no stride allowed and the interval is open on the
right:
foreach (i; 0 .. 100)
2) Using iota, a badly named range, for intervals open on the right, it
supports an optional stride:
foreach (i; iota(100))
But empty intervals have different semantics. This runs with no errors, the
foreach doesn't loop:
void main() {
foreach (i; 0 .. -1) {}
}
Looks like a bug to me.
3) The switch range syntax, it uses two dots still, it's closed on the right:
import std.range;
void main() {
char c = 'z';
bool good;
switch (c) {
case 'a': .. case 'z':
good = true;
break;
default:
good = false;
break;
}
assert(good);
}
But the syntax is NOT case a..b, it's case a .. case b.
a..b always has intervals open on the right. There are no exceptions.