Array Indexing/Slicing Range Checking, Exceptions and @nogc

Nordlöw via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 30 06:12:49 PDT 2016


I'm working on a support-it-all Array implementation (with 
optional sortedness) at

https://github.com/nordlow/justd/blob/master/packedarray.d

that has a very flexible memory allocation model via either 
GC.malloc or via privately imported malloc, realloc and free. 
This in order to provide pure @trusted access in all cases except 
slicing and `auto ref opIndex`.

I'm however uncertain about how to implement error handling of 
bounds checking and how these interact with `@nogc`. My main goal 
is to match semantics of builtin D arrays and slices. If so 
should we implement bounds checking as either

opIndex(size_t index) nothrow @nogc
{
     assert(index < length, "Index out of bounds");
}

or via

opIndex(size_t index) // cannot be nothrow @nogc
{
     if (index >= length) { throw new RangeError("Index out of 
bounds"); }
}

?

What's the preferred policy here?


More information about the Digitalmars-d-learn mailing list