On Friday, 23 April 2021 at 18:35:25 UTC, Paul Backus wrote:
On Friday, 23 April 2021 at 17:35:13 UTC, Bastiaan Veelo wrote:
What happens when a range is released?
What happens if a range is not released?
What happens if a range is released more than once?
And what does "controlled" imply here? In what way has
`SortedRange` control over the underlaying data? What can I do
and what can't I do to the underlying data as long as
`SortedRange` has control?
I had to look at the source to figure this out. Fortunately,
the source is pretty simple:
https://phobos.dpldocs.info/source/std.range.d.html#L10833
Turns out, it just calls `core.lifetime.move`. So I guess when
the docs say "control", what they are really talking about is
ownership.
Practically speaking, this means that in generic code, we
should avoid using a `SortedRange` after calling `release`,
since we do not know whether the range is owning or non-owning
w.r.t. the underlying data.
Thanks. There is no word in the documentation about when
`SortedRange` would or would not be owning data, though, and
hence when `release` would be appropriate to call. To me
`release` seems to be calling `std.algorithm.mutation.move` , but
it is probably very similar to `core.lifetime.move`.
In case the underlying data is a slice, I think we end up at
https://phobos.dpldocs.info/source/std.algorithm.mutation.d.html#L1459, which I can't see accomplishes anything in [this example](https://dlang.org/phobos/std_algorithm_sorting.html#.sort) (except returning the slice that went in):
```d
int[] array = [ 1, 2, 3, 4 ];
// sort in descending order
array.sort!("a > b");
writeln(array); // [4, 3, 2, 1]
// sort in ascending order
array.sort();
writeln(array); // [1, 2, 3, 4]
// sort with reusable comparator and chain
alias myComp = (x, y) => x > y;
writeln(array.sort!(myComp).release); // [4, 3, 2, 1]
```
-- Bastiaan.