Referencer
Alex Parrill via Digitalmars-d
digitalmars-d at puremagic.com
Fri Nov 20 10:48:49 PST 2015
On Friday, 20 November 2015 at 18:23:57 UTC, HaraldZealot wrote:
> All ranges in Phobos pass by value, but if I have output range
> with state like cumulative statistics this is useless.
>
> After discussion with Dicebot I try this work-arround:
> http://dpaste.dzfl.pl/8af8eb8d0007
>
> It is unfinished. But direction is observable.
>
> Is this good solution? And how about to include something like
> this tool in Phobos?
I'm not sure how useful this is as opposed to plain pointers. For
structs, since `foo.bar` is the same as `(&foo).bar`, you may as
well use a pointer, and the only thing it saves for numbers is a
pointer deference or two.
You say ranges are pass-by-value, but that's not entirely true.
Ranges themselves can be classes, or be made references via
std.range.refRange. Range elements can be made lvalues by using
ref functions [1].
As for the code:
* Your example usage (x = x += x = (x * 5)) is confusing, due to
the chained assignments.
* I wouldn't mark this struct as @safe because the passed value
may leave scope, causing invalid dereferences.
* There's no point in making the Reference struct a template, as
the function its defined in is also a template. Just replace
usages of U with T.
[1]: http://dlang.org/function.html#ref-functions see also
std.range.interface.hasLvalueElements
More information about the Digitalmars-d
mailing list