Playing with ranges and ufcs

Andrea Fontana nospam at example.com
Wed Feb 27 15:21:14 PST 2013


On Wednesday, 27 February 2013 at 22:48:06 UTC, bearophile wrote:
> 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

Done!


More information about the Digitalmars-d-learn mailing list