Passing $ as a function argument
Michael Coulombe
kirsybuu at gmail.com
Sun Oct 14 15:27:07 UTC 2018
On Sunday, 14 October 2018 at 14:35:36 UTC, lngns wrote:
> On Sunday, 14 October 2018 at 13:18:37 UTC, lngns wrote:
>> That would require introducing a new type
>
> Or just use int with a negative number... That's how it's done
> in some dynamic languages.
> But my point is that it should be compatible with pre-existing
> code using unsigned indices somehow. I don't think that is
> possible.
Another way to do this with UFCS:
// write-once library wrapper
struct Indexer(R) {
R r;
auto opDollar() { return r.length; }
auto opIndex(size_t i) { return r[i]; }
}
auto indexer(R)(R r) { return Indexer(r); }
// rewrite index parameter => return wrapped range
auto foo()
{
auto arr = (...);
return indexer(arr);
}
// easy-to-use result
auto end = foo[$-1];
More information about the Digitalmars-d
mailing list