diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 4a35c1a2eb9db..db4a102221e5f 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1198,10 +1198,12 @@ end """ copymutable(a) -Make a mutable copy of an array or iterable `a`. For `a::Array`, -this is equivalent to `copy(a)`, but for other array types it may -differ depending on the type of `similar(a)`. For generic iterables -this is equivalent to `collect(a)`. +Make a mutable copy of an array or iterable `a`. The result should have the same content as +`a`, but support mutation. For `Array`s and most iterables this is equivalent to +`collect(a)`, but for other array types it may differ depending on the type of `similar(a)`. + +To change the behavior of `copymutable` on a custom collection, define [`similar`](@ref) +for that collection. # Examples ```jldoctest @@ -1213,6 +1215,20 @@ julia> Base.copymutable(tup) 1 2 3 + +julia> using StaticArrays + +julia> s = SVector{3}(1,2,3) +3-element SVector{3, Int64} with indices SOneTo(3): + 1 + 2 + 3 + +julia> Base.copymutable(s) +3-element MVector{3, Int64} with indices SOneTo(3): + 1 + 2 + 3 ``` """ function copymutable(a::AbstractArray) diff --git a/base/exports.jl b/base/exports.jl index b6f7ea0d6ad35..c77421d5b1b68 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -1114,6 +1114,7 @@ public split_rest, tail, checked_length, + copymutable, # Loading DL_LOAD_PATH, diff --git a/base/sort.jl b/base/sort.jl index f35add7a55b35..7d5f7d0d1f00a 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -1475,7 +1475,8 @@ end Variant of [`sort!`](@ref) that returns a sorted copy of `v` leaving `v` itself unmodified. Returns something [`similar`](@ref) to `v` when `v` is an `AbstractArray` and uses -[`collect`](@ref) to support arbitrary non-`AbstractArray` iterables. +[`collect`](@ref) to support arbitrary non-`AbstractArray` iterables. some types have +specialized versions of `sort`, such as `NTuples` (`sort(::NTuple)` returns an `NTuple`) !!! compat "Julia 1.10" `sort` of arbitrary iterables requires at least Julia 1.10. diff --git a/doc/src/base/collections.md b/doc/src/base/collections.md index a8f2bdc6b7d7d..5baf0125f6e4f 100644 --- a/doc/src/base/collections.md +++ b/doc/src/base/collections.md @@ -137,6 +137,7 @@ Base.tail Base.step Base.collect(::Any) Base.collect(::Type, ::Any) +Base.copymutable(::Any) Base.filter Base.filter! Base.replace(::Any, ::Pair...)