why $ is need to access array [negative index]?

claptrap clap at trap.com
Sun Sep 20 14:01:58 UTC 2020

On Friday, 18 September 2020 at 21:31:53 UTC, IGotD- wrote:
> On Friday, 18 September 2020 at 20:46:00 UTC, mw wrote:
>> Currently we have range check on every single indexing 
>> operation already; so the trade-off here is: adding one more 
>> check v.s. the convenience it buys.
> We want as few range checks as possible because indexing arrays 
> are not too seldom done it loops. Adding an extra check could 
> affect performance in this case.

You dont need an extra check, you just do an unsigned comparison, 
negative values will interpreted as larger. IE...

int idx = length-i;
assert(cast(unsigned) idx < length)

catches anything outside the valid range.

Yes you have the issue that the index could wrap around and end 
up valid, but you already have that anyway. I mean if (idx-1) is 
going to cause a problem so will (idx+0xFFFFFFFF)

More information about the Digitalmars-d mailing list