Range violation instead of empty slice on a[3 .. 2]

SimonN via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 21 10:03:05 PST 2015


     string a = "hello";
     string b = a[3 .. 2];

I expect b to become an empty slice, because 3 is >= 2 already 
after 0 increments, making the slice length 0. Instead, the code 
throws a range violation.

Expressions of this kind come up, e.g., when taking slices near 
the end of arrays, like "slice = a[b.length .. $];". To make this 
robust, I need an extra check for b.length > a.length, returning 
null in this case, otherwise a[b.length .. $].

What's the design reason to prefer throwing over returning an 
empty slice?

-- Simon


More information about the Digitalmars-d-learn mailing list