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

Ethan gooberman at gmail.com
Fri Sep 18 22:01:56 UTC 2020

On Friday, 18 September 2020 at 21:06:22 UTC, mw wrote:
> You are indeed confused: the justification is either compiler 
> writes that branching code, or user have to write it.


Let the user write it every time.

I don't want the compiler inserting branches in to my code simply 
because of the essentially-never chance I might not want to put a 
$ in front of a -<integer> statement for array access.

Further to write it every time: Write an array template. It's 
simple. Here's the important parts, the rest isn't too difficult 
to write from here

struct Array( T )
   ref auto opIndex( int Index ) { if( Index < 0 ) Index = 
slice.length + Index; return slice[ Index ]; }
   T[] slice;

More information about the Digitalmars-d mailing list