opIndex negative index?

tsbockman thomas.bockman at gmail.com
Thu Jan 21 22:41:26 UTC 2021


On Thursday, 21 January 2021 at 14:00:28 UTC, cerjones wrote:
> I'm a bit unsure if this is reasonable or not.
>
> Thoughts?

Signed indexes are fine, if there is a principled reason to use 
them. Just make sure that you are consistent by making `length`, 
`opDollar`, etc. use signed types, as well.

Otherwise, you're laying a trap for users of the API who may 
accidentally write code with mixed signed-unsigned integer 
operations. That's a big problem, because mixed signed-unsigned 
integer operations are done incorrectly by default, often with no 
warnings, in D and some other C-like languages.

Consistently using signed types for lengths and indexes will make 
the API incompatible with some Phobos (D's `std`) range code, but 
you should get a compile-time error message. Mixing signed and 
unsigned, on the other hand, is likely to result in subtly wrong 
code that compiles without error, but sometimes does the wrong 
thing.


More information about the Digitalmars-d-learn mailing list