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

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Sep 18 21:26:04 UTC 2020


On Fri, Sep 18, 2020 at 09:14:19PM +0000, mw via Digitalmars-d wrote:
> On Friday, 18 September 2020 at 21:11:58 UTC, Steven Schveighoffer wrote:
> > Well, if you don't care about verbosity.
> > 
> > signs[(sign + $) % $] = ...;
> 
> I want compiler writes this verbosity, instead of the user :-)

Yawn.

	struct MyArray(T) {
		T[] impl;
		alias impl this;
		this(T[] data) { impl = data; }
		ref T opIndex(ptrdiff_t idx) {
			return (idx < 0) ? impl[$ + idx] : impl[idx];
		}
	}

	MyArray!int x = [ 1, 2, 3 ];
	assert(x[-1] == 3);	// not verbose anymore


T

-- 
Живёшь только однажды.


More information about the Digitalmars-d mailing list