Passing $ as a function argument

Vladimir Panteleev thecybershadow.lists at gmail.com
Thu Oct 11 01:27:51 UTC 2018


On Wednesday, 10 October 2018 at 08:46:42 UTC, James Japherson 
wrote:
> Would be nice to be able to pass $ as a function argument to be 
> used in automatic path length traversing.

You can already do this, by returning a custom type from opDollar:

/// Define RealNumbers so that, given `RealNumbers r`, `r[x] == 
x` but `r[$] == real.infinity`.
struct RealNumbers
{
	private struct Dollar {}
	Dollar opDollar() { return Dollar.init; }
	real opIndex(size_t index) { return index; }
	real opIndex(Dollar dollar) { return real.infinity; }
}

unittest
{
	RealNumbers r;
	assert(r[5] == 5);
	assert(r[$] == real.infinity);
}



More information about the Digitalmars-d mailing list