Playing with ranges and ufcs

Jonathan M Davis jmdavisProg at gmx.com
Wed Feb 27 15:17:09 PST 2013


On Wednesday, February 27, 2013 23:48:05 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);
> }
> 

What it should be doing is using version(assert) to throw a RangeError if the 
arguments are invalid, but the addition of version(assert) is quite recent 
(previously, the best that could have been done was to assert). That's the 
general policy, but it's not always followed like it should be.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list