www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Built-in range type

reply Lionello Lunesu <lio lunesu.remove.com> writes:
Slices are one of D's greatest assets, and now in D2 we have the new
foreach range statement: foreach(i; 0..22) { ... }

Wouldn't it be nice to introduce a built-in for these int..int ranges?

auto range = 0..22;
auto slice = array[range];
foreach(i; range) {...}

L.
Apr 28 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Lionello Lunesu:

Wouldn't it be nice to introduce a built-in for these int..int ranges?<
It was discussed in the past, but there are some design problems. In theory it can be used to define overload of slices operator as: opSliceAssign(int v, Slice s) A problem is that slices can have more than just size_t indices, this code works and 'i' is of type long: import std.stdio: writeln; void main() { foreach (i; (long.max - 10) .. (long.max - 1)) writeln(typeid(typeof(i)), " ", i); } So the (immutable) Slice struct has to be a template: opSliceAssign(int v, Slice!size_t s) struct Slice(TIndex) { immutable TIndex start, stop; immutable int stride; // simplified immutable SliceType type; this(...) {...} // few methods: in operator, length, subslicing, etc. } So it's usually 4 CPU words long on 32 bit CPUs (it can be just 3 words long on 64 bit CPUs if stride and type are presented in 32 bit each). The stride is the third optional value, usually it's 1, its syntax can be: start .. stop : stride The stride is an int because it can be negative. If start and stop are long, you can't have a stride larger than an int min-max values. 'type' is a bitfield that encodes the kind of Slice. I think the cases are: 1 .. 7 0 .. $ 0 .. $-10 1 .. 7 : 2 0 .. $ : 2 0 .. $-10 : 2 .. 7 .. $ .. $-10 .. 7 : 2 .. $ : 2 .. $-10 : 2 (The cases with no start are meant to be useful for intervals infinite on the left. I am not sure this is an important group of cases.) The Chapel language generalizes this idea into the idea of "domain", a first-class index set. I have explained it here, and I think it's an useful idea: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=87311 Bye, bearophile
Apr 28 2010
parent Lionello Lunesu <lio lunesu.remove.com> writes:
I appreciate your comments.

The introduction of a range type (as they appear in slices and foreach)
should not have to introduce new features. A simple int..int (or
uint/long/ulong) 'tuple' would do the trick. Internally the compiler
already has SliceExp with 'lwr' and 'upr' bounds. Exposing this to the
programmer might be useful.

Anyway, probably not useful enough :)

L.

On 29-4-2010 1:04, bearophile wrote:
 Lionello Lunesu:
 
 Wouldn't it be nice to introduce a built-in for these int..int ranges?<
It was discussed in the past, but there are some design problems. In theory it can be used to define overload of slices operator as: opSliceAssign(int v, Slice s) A problem is that slices can have more than just size_t indices, this code works and 'i' is of type long: import std.stdio: writeln; void main() { foreach (i; (long.max - 10) .. (long.max - 1)) writeln(typeid(typeof(i)), " ", i); } So the (immutable) Slice struct has to be a template: opSliceAssign(int v, Slice!size_t s) struct Slice(TIndex) { immutable TIndex start, stop; immutable int stride; // simplified immutable SliceType type; this(...) {...} // few methods: in operator, length, subslicing, etc. } So it's usually 4 CPU words long on 32 bit CPUs (it can be just 3 words long on 64 bit CPUs if stride and type are presented in 32 bit each). The stride is the third optional value, usually it's 1, its syntax can be: start .. stop : stride The stride is an int because it can be negative. If start and stop are long, you can't have a stride larger than an int min-max values. 'type' is a bitfield that encodes the kind of Slice. I think the cases are: 1 .. 7 0 .. $ 0 .. $-10 1 .. 7 : 2 0 .. $ : 2 0 .. $-10 : 2 .. 7 .. $ .. $-10 .. 7 : 2 .. $ : 2 .. $-10 : 2 (The cases with no start are meant to be useful for intervals infinite on the left. I am not sure this is an important group of cases.) The Chapel language generalizes this idea into the idea of "domain", a first-class index set. I have explained it here, and I think it's an useful idea: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=87311 Bye, bearophile
Apr 29 2010