is this considered inconsistency?

Dennis dkorpel at gmail.com
Sun Mar 21 16:47:41 UTC 2021


On Sunday, 21 March 2021 at 15:55:04 UTC, mw wrote:
> Why can't the a[0 .. -1] just return empty range, when end <= 
> start, as in a[0 ..  0]? and also in foreach (i;   0 .. -1 )?

In `foreach (i; 0 .. -1)`, `i` is typed `int`. In a slice, the 
indices are of type `size_t` which is an unsigned integer type. 
-1 underflows to `ulong.max` (on 64-bit) which is why you get a 
range violation.

You could define your own slice type as a struct with operator 
overloads, where you can use signed indices or automatic clamping 
of the bounds or whatever you want. Changing the behavior of the 
language type would be a breaking and possibly controversial 
change.

There's also a point to be made that the implicit conversion from 
signed to unsigned integers should not be allowed, that's a 
separate discussion.


More information about the Digitalmars-d mailing list