diff --git a/NEWS.md b/NEWS.md index cdfd5c190680bc..90bf58a5182393 100644 --- a/NEWS.md +++ b/NEWS.md @@ -646,7 +646,9 @@ Deprecated or removed * Using Bool values directly as indices is now deprecated and will be an error in the future. Convert them to `Int` before indexing if you intend to access index `1` for `true` and `0` for `false`. - * `slicedim` has been deprecated in favor of `selectdim` ([#26009]). + * `slicedim(A, d, i)` has been deprecated in favor of `copy(selectdim(A, d, i)`. The new + `selectdim` function now always returns a view into `A`; in many cases the `copy` is + not necessary ([#26009]). * `whos` has been renamed `varinfo`, and now returns a markdown table instead of printing output ([#12131]). diff --git a/base/abstractarraymath.jl b/base/abstractarraymath.jl index f281d386f87df2..1a3b8ef3d694ab 100644 --- a/base/abstractarraymath.jl +++ b/base/abstractarraymath.jl @@ -100,8 +100,9 @@ imag(x::AbstractArray{<:Real}) = zero(x) """ selectdim(A, d::Integer, i) -Return all the data of `A` where the index for dimension `d` equals `i`. Equivalent to -`A[:,:,...,i,:,:,...]` where `i` is in position `d`. +Return a view of all the data of `A` where the index for dimension `d` equals `i`. + +Equivalent to `view(A,:,:,...,i,:,:,...)` where `i` is in position `d`. # Examples ```jldoctest @@ -110,17 +111,18 @@ julia> A = [1 2 3 4; 5 6 7 8] 1 2 3 4 5 6 7 8 -julia> selectdim(A,2,3) -2-element Array{Int64,1}: +julia> selectdim(A, 2, 3) +2-element view(::Array{Int64,2}, Base.OneTo(2), 3) with eltype Int64: 3 7 ``` """ -function selectdim(A::AbstractArray, d::Integer, i) +@inline selectdim(A::AbstractArray, d::Integer, i) = _selectdim(A, d, i, setindex(axes(A), i, d)) +@noinline function _selectdim(A, d, i, idxs) d >= 1 || throw(ArgumentError("dimension must be ≥ 1")) nd = ndims(A) - d > nd && (i == 1 || throw_boundserror(A, (ntuple(k->Colon(),nd)..., ntuple(k->1,d-1-nd)..., i))) - A[setindex(axes(A), i, d)...] + d > nd && (i == 1 || throw(BoundsError(A, (ntuple(k->Colon(),d-1)..., i)))) + return view(A, idxs...) end """ diff --git a/base/deprecated.jl b/base/deprecated.jl index 4e847df9d676c2..73ecb6cc9f23a3 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -26,7 +26,7 @@ macro deprecate(old, new, ex=true) newname = Expr(:quote, new) Expr(:toplevel, ex ? Expr(:export, esc(old)) : nothing, - :(function $(esc(old))(args...) + :(@__doc__ function $(esc(old))(args...) $meta depwarn($"`$old` is deprecated, use `$new` instead.", $oldmtname) $(esc(new))(args...) @@ -51,7 +51,7 @@ macro deprecate(old, new, ex=true) end Expr(:toplevel, ex ? Expr(:export, esc(oldsym)) : nothing, - :($(esc(old)) = begin + :($(@__doc__ esc(old)) = begin $meta depwarn($"`$oldcall` is deprecated, use `$newcall` instead.", $oldmtname) $(esc(new)) @@ -1360,8 +1360,21 @@ end # issue #25928 @deprecate wait(t::Task) fetch(t) -# PR #26008 -@deprecate slicedim(A::AbstractArray, d::Integer, i) selectdim(A, d, i) +" +`slicedim` is deprecated in favor of `selectdim`. Whereas `slicedim` previously +used `getindex` to select its data, `selectdim` now always uses `view`. +" +@deprecate slicedim(A::AbstractArray, d::Integer, i) copy(selectdim(A, d, i)) +function slicedim(A::AbstractVector, d::Integer, i::Number) + if d == 1 + # slicedim would have returned a scalar value, selectdim always returns views + depwarn("`slicedim(A::AbstractVector, d::Integer, i)` is deprecated, use `selectdim(A, d, i)[]` instead.", :slicedim) + selectdim(A, d, i)[] + else + depwarn("`slicedim(A::AbstractArray, d::Integer, i)` is deprecated, use `copy(selectdim(A, d, i))` instead.", :slicedim) + copy(selectdim(A, d, i)) + end +end # PR 25062 @deprecate(link_pipe(pipe; julia_only_read = true, julia_only_write = true),