# Proposal to update to sortperm signature

**URL:** <https://discourse.julialang.org/t/proposal-to-update-to-sortperm-signature/7302>\
**Category:** Internals & Design\
**Tags:** proposal, sort, sortperm\
**Created:** [November 25, 2017, 11:18am UTC](https://discourse.julialang.org/t/proposal-to-update-to-sortperm-signature/7302 "2017-11-25T11:18:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [November 25, 2017, 11:18am UTC](https://discourse.julialang.org/t/proposal-to-update-to-sortperm-signature/7302/1 "2017-11-25T11:18:33Z")

</div>

I have written [a faster `sortperm` for Radixsort](https://github.com/JuliaCollections/SortingAlgorithms.jl/pull/25) (defined in SortingAlgorithms.jl).

However I encountered a number of difficulties in extending the `sortperm` from `Base`, the main reason is that the signature for `sortperm` is `sortperm(v; alg::Algorithm, ...)`, so I if I define `sort(v; alg::RadiSortAlg)` then it will override the `Base.sort` when I do `sort(v)`.

So I think it’s better to have an additional `sortperm` signature that looks like `sortperm(v, alg::Algorithm; ...)`. This way I can define `sortperm(v, alg::RadixSortAlg)`.

This is the main impetus behind [my PR to Base’s `sortperm` signatures](https://github.com/JuliaLang/julia/pull/24772).

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 25, 2017, 2:06pm UTC](https://discourse.julialang.org/t/proposal-to-update-to-sortperm-signature/7302/2 "2017-11-25T14:06:31Z")

</div>

The main issue here is keyword args don’t dispatch. But the true fix is… make keyword arguments able to dispatch. Step 1 was implementing NamedTuples. Step 2 is making keyword args use NamedTuples so that way they are concretely typed (that will give them speed). Step 3 is to allow it to dispatch off of the type signatures. I’d much rather see it just happen the right way than fudge APIs for the time being.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 25, 2017, 4:18pm UTC](https://discourse.julialang.org/t/proposal-to-update-to-sortperm-signature/7302/3 "2017-11-25T16:18:27Z")

</div>

I think that is not on the books for 1.0 though, at least the performance part: [https://github.com/JuliaLang/julia/issues/9551](https://github.com/JuliaLang/julia/issues/9551). Which presumably also means the dispatch (edit: also dispatch would be breaking, I think, thus 2.0).

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 25, 2017, 4:23pm UTC](https://discourse.julialang.org/t/proposal-to-update-to-sortperm-signature/7302/4 "2017-11-25T16:23:34Z")

</div>

Step 1 is complete of course. I’m not worried about the performance part: it’s quick not hard to implement yourself with a macro and NamedTuples.jl (see Keys.jl) so it should not be difficult to do in Base. As for dispatching, I am not sure how that stuff is implemented in Base so I don’t know exactly how difficult it would be, but this doesn’t seem like it’s so far away.

> [@mauro3](#):
>
> also dispatch would be breaking, I think, thus 2.0

Would it be breaking? It didn’t have a meaning before other than a type-assertion. But if things failed the type-assertion, then I believe it would just get a method error which is the same that dispatch would do. It would then just allow you to define a different dispatch, which is right now just overwriting the method so I don’t believe that actually has a use right now. I would go even further than that: I think that most people who put an assertion on keyword args might not know that it’s not actually dispatching (I didn’t know for a long time), so in some sense it’s a bugfix.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 25, 2017, 4:45pm UTC](https://discourse.julialang.org/t/proposal-to-update-to-sortperm-signature/7302/5 "2017-11-25T16:45:18Z")

</div>

Yes, you’re right, it shouldn’t be breaking in-itself. But updating an API to make use of it might well be (as updating any API really is potentially breaking).

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 25, 2017, 4:51pm UTC](https://discourse.julialang.org/t/proposal-to-update-to-sortperm-signature/7302/6 "2017-11-25T16:51:49Z")

</div>

> [@mauro3](#):
>
> But updating an API to make use of it might well be (as updating any API really is potentially breaking).

If the APIs are asserting on an abstract type like this is doing, then people can extend it without breaking the API just by dispatching on a concrete subtype. So this could be extended by adding keyword argument dispatch and then adding a new Algorithm type dispatch without breaking the existing API.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 26, 2017, 8:22pm UTC](https://discourse.julialang.org/t/proposal-to-update-to-sortperm-signature/7302/7 "2017-11-26T20:22:43Z")

</div>

Well, well:  
[https://github.com/JuliaLang/julia/pull/24795](https://github.com/JuliaLang/julia/pull/24795)
