Forward ranges in Phobos v2

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Nov 1 23:46:07 UTC 2021


On Mon, Nov 01, 2021 at 11:34:20PM +0000, Alexandru Ermicioi via Digitalmars-d wrote:
> On Monday, 1 November 2021 at 14:13:00 UTC, H. S. Teoh wrote:
> > If you have a struct but it only supports input range semantics,
> > then you could pass it by ref or pass a pointer to it.
> > 
> > If you have a class but it supports forward range semantics, then
> > wrap it in a struct with a copy ctor that saves state in the copy
> > ctor.
[...]
> What about random access range?

Presumably, it will be a struct with additional methods needed for
random access (opIndex, et al).


> If you don't care for the implementation of the range but only being a
> forward range, how would you express it as a parameter for a method?
[...]

Good question, ask Andrei. ;-)

Presumably, if we standardize on structs/classes, it could be as simple
as:

	auto myFunc(R)(R range) if (is(R == struct)) {
		... // forward range
	}

	auto myFunc(R)(R range) if (is(R == class)) {
		... // input range
	}

But given that Andrei thinks it's a mistake for ranges to be implemented
as classes, I've no idea.


T

-- 
Don't modify spaghetti code unless you can eat the consequences.


More information about the Digitalmars-d mailing list