sort API design

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Nov 20 13:35:58 UTC 2019


On Wednesday, November 20, 2019 6:15:12 AM MST Dukc via Digitalmars-d wrote:
> On Wednesday, 20 November 2019 at 09:33:25 UTC, Johannes Riecken
>
> wrote:
> > Later I learned that I was trapped by `sort` being in-place but
> > still returning a value. If this API were to be designed from
> > scratch, I wonder if it would be possible to rule out this
> > incorrect usage at compile-time while still retaining the other
> > nice features of the `sort` function like being in-place and
> > giving strong types which can be used to choose faster
> > algorithms to further transform the result, etc.?
>
> Theoretically it could do the same thing as `std.algortihm.move`
> - assign the original range to it's init value, so if it's
> accidently reused it'd be more obvious. But of course it won't be
> worth doing anymore because of the breakage. Perhaps in the std
> v2 Wilzbach has talked about, if the authors so wish.

The fact that sort returns a SortedRange is useful for some algorithms
(since they can then statically detect that the range is sorted), but it's
also useful to be able to use sort the range in-place and continue to use it
with the exact same type. The current API is only a problem if you don't
read the documentation.

- Jonathan M Davis





More information about the Digitalmars-d mailing list