Difference between range `save` and copy constructor
Steven Schveighoffer
schveiguy at gmail.com
Sat Feb 15 14:34:42 UTC 2020
On 2/15/20 5:53 AM, uranuz wrote:
> I am interested in current circumstances when we have new copy
> constructor feature what is the purpose of having range `save`
> primitive? For me they look like doing basicaly the same thing. And when
> looking in some source code of `range` module the most common thing that
> `save` does is that it use constructor typeof(this) to create a new
> instance and use `save` on the source range:
> https://github.com/dlang/phobos/blob/v2.090.1/std/range/package.d
>
> So what is conceptual difference between `save` and copy contructor of
> range?
Nothing. IMO, any time you are doing anything in save other than `return
this;`, you shouldn't have implemented it.
The original impetus for the save requirement was so that forward ranges
could have a tangible checkable thing that allows introspection (does
the range have a save method?).
I'm not entirely sure if disabled postblit was even available at the time.
The correct way to do it would be to treat ranges that can be copied
(regardless of whether they have a copy constructor) as forward ranges,
and treat ones that cannot be copied as input ranges.
But it's hard to redo ranges like this with all existing code out there.
-Steve
More information about the Digitalmars-d-learn
mailing list