Must ranges have value semantics?
Chris Wright via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Dec 15 09:36:52 PST 2015
I noticed that some methods in Phobos will have very different behavior
with forward ranges that have reference semantics and those that have
value semantics.
Example:
auto range = getSomeRange();
auto count = range.walkLength;
foreach (element; range) { writeln(element); }
If getSomeRange returns a forward range that is a reference type with
no .length property, walkLength will exhaust the range. The iteration
after that will never enter the loop body.
However, if getSomeRange returns a range with value semantics with
no .length property, then iteration is still possible.
I haven't found documentation about how ranges are intended to be used in
D written by people who maintain Phobos. Is it normal and expected that I
should have to call .save everywhere, manually? Was there discussion on
whether this should be the case or a published document containing the
reasoning behind the decision?
More information about the Digitalmars-d-learn
mailing list