Playing with ranges and ufcs

bearophile bearophileHUGS at lycos.com
Wed Feb 27 14:48:05 PST 2013


Andrea Fontana:

> writeln(iota(10).cycle()[5..2].take(4));
>
> print:
>
> [5, 6, 7, 8]
>
>
> Shouldn't [5..2] slice throw a compile/runtime error?

Please file it in bugzilla.

The opSlice of cycle() lacks those pre-conditions or tests, and 
there are not enough unittests in Phobos to catch this simple bug:

         auto opSlice(size_t i, size_t j)
         {
             auto retval = this.save;
             retval._index += i;
             return takeExactly(retval, j - i);
         }


j - i is positive because those numbers are unsigned, and because 
D lacks a run-time errors for integral values because the stupid 
Walter thinks those run-time are too much slow (it's not actually 
true. Even Clang designers have understood it), and Don thinks 
that Everything is Fine in D:


import std.stdio;

struct Foo {
     auto opSlice(size_t i, size_t j) {
         writeln(j - i); // Prints: 4294967293
     }
}

void main() {
     Foo f;
     f[5 .. 2];
}



Maybe we need to run something similar to QuickCheck 
(http://en.wikipedia.org/wiki/QuickCheck ) on Phobos.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list