www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - When should I use SortedRange.release?

reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
For reference, `SortedRange.release` is 
[documented](https://dlang.org/phobos/std_range.html#.SortedRange) as such:

"Releases the controlled range and returns it."

Wow thanks! I love functions that are named exactly as what they 
do ;-) Seriously though, I still don't know what it is that it 
does, and why and when it should be done, and when not.

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?

My failure to understand this function makes me fear I don't 
understand `SortedRange` at all, and thereby don't understand how 
to use `algorithm.sorting` properly.

Thanks!

-- Bastiaan.
Apr 23 2021
next sibling parent reply Paul Backus <snarwin gmail.com> writes:
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.
Apr 23 2021
parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
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.
Apr 23 2021
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 4/23/21 1:35 PM, Bastiaan Veelo wrote:
 For reference, `SortedRange.release` is 
 [documented](https://dlang.org/phobos/std_range.html#.SortedRange) as such:
 
 "Releases the controlled range and returns it."
 
 Wow thanks! I love functions that are named exactly as what they do ;-) 
 Seriously though, I still don't know what it is that it does, and why 
 and when it should be done, and when not.
 
 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?
 
 My failure to understand this function makes me fear I don't understand 
 `SortedRange` at all, and thereby don't understand how to use 
 `algorithm.sorting` properly.
 
`SortedRange` itself is kind of a kludge of "policy" that isn't fit for function. I use release to get the original data type out (via r.save.release), but that's about it. See my rant about it [here](https://forum.dlang.org/post/r7ia94$19uo$1 digitalmars.com) -Steve
Apr 23 2021
parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Friday, 23 April 2021 at 21:34:39 UTC, Steven Schveighoffer 
wrote:
 `SortedRange` itself is kind of a kludge of "policy" that isn't 
 fit for function.

 I use release to get the original data type out (via 
 r.save.release), but that's about it.

 See my rant about it 
 [here](https://forum.dlang.org/post/r7ia94$19uo$1 digitalmars.com)

 -Steve
Understood, thanks. --Bastiaan.
Apr 23 2021