Why does sort not work on fixed arrays?
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Tue Sep 17 21:32:48 UTC 2019
On Tuesday, September 17, 2019 3:23:53 PM MDT Brett via Digitalmars-d wrote:
> T[N] t;
> sort(t); fails
>
> but
>
> T[] t;
> sort(t); passes
>
>
> but magically
>
> T[N] t;
> sort(t[0..$]); passes !!!
sort operates on random-access ranges. Static arrays are not ranges. In
order for them to be ranges, it would have to be possible to pop elements
off of them, and that obviously won't work when the type has a fixed number
of elements. Slicing a static array results in a dynamic array which is a
slice of the static array, and that _is_ a random-access range, so it works
with sort.
BTW, if you want to slice the entirety of a static array, you don't need
indices. You can just do t[] rather than t[0..$].
- Jonathan M Davis
More information about the Digitalmars-d
mailing list