I'm confused about ranges (input and forward in practice, in algorithms)
Gary Willoughby via Digitalmars-d
digitalmars-d at puremagic.com
Fri Aug 14 00:28:57 PDT 2015
On Friday, 14 August 2015 at 00:33:30 UTC, Luís Marques wrote:
> Despite using them all the time, I'm suddenly confused about
> ranges...
>
> My understanding is that (for library-worth code) algorithms
> that consume ranges are supposed to use .save() to be
> compatible with both ref type and value type ranges. What about
> the reverse? Are algorithms supposed to try to avoid
> effectively .save()ing ranges (when they are value types) by
> not copying range variables? Furthermore, is it incorrect for
> an input range that is also not a forward range (or at least
> does not declare itself formally as a forward range by having a
> save()) to allow itself to be bitwise copied? (i.e., to
> effectively provide a save() behaviour).
I was confused a lot about ranges until I realised that a range
is only a `view` onto the data. A range is not *the* data.
For example, consuming a range does not consume the underlying
data or modify it in any way. A forward range's save method is
for creating a copy of the view, not a copy of the underlying
data. This is to allow two ranges to be consumed independently
without affecting the data.
Usually, these ranges are returned in the form of a Result struct
as shown here:
https://github.com/D-Programming-Language/phobos/blob/master/std/uni.d#L6517
More information about the Digitalmars-d
mailing list