Playing with ranges and ufcs

bearophile bearophileHUGS at lycos.com
Wed Feb 27 15:35:08 PST 2013


Jonathan M Davis:

> 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).

Do you mean something like this?

auto opSlice(size_t i, size_t j) {
     version(assert) {
         if (i > j)
             throw new RangeError("some message here");
     }
     auto retval = this.save;
     retval._index += i;
     return takeExactly(retval, j - i);
}


What's the advantage of that compared to using a normal contract?

auto opSlice(size_t i, size_t j)
in {
     assert(i <= j, "some message here");
} body {
     auto retval = this.save;
     retval._index += i;
     return takeExactly(retval, j - i);
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list