On Tuesday, 15 December 2015 at 17:36:52 UTC, Chris Wright wrote:
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?

Unfortunately you must make no assumption about the ranges state in generic code after calling a function that takes the range by value. If you want to perform other operations after the call to walklength, you'll need to have a new range created with save() before that call.

The value vs reference rage creates challenges, but in practice it will be something you occasionally hit.

I kind of feel like a range should always be taken by reference so that it is predictable that the range will be modified if it is value or reference semantics.

Reply via email to