@@ -818,15 +818,18 @@ julia> similar(falses(10), Float64, 2, 4)
818818See also: [`undef`](@ref), [`isassigned`](@ref).
819819"""
820820similar (a:: AbstractArray{T} ) where {T} = similar (a, T)
821- similar (a:: AbstractArray , :: Type{T} ) where {T} = similar (a, T, to_shape ( axes (a) ))
822- similar (a:: AbstractArray{T} , dims:: Tuple ) where {T} = similar (a, T, to_shape ( dims) )
823- similar (a:: AbstractArray{T} , dims:: DimOrInd... ) where {T} = similar (a, T, to_shape ( dims) )
824- similar (a:: AbstractArray , :: Type{T} , dims:: DimOrInd... ) where {T} = similar (a, T, to_shape ( dims) )
821+ similar (a:: AbstractArray , :: Type{T} ) where {T} = similar (a, T, axes (a))
822+ similar (a:: AbstractArray{T} , dims:: Tuple ) where {T} = similar (a, T, dims)
823+ similar (a:: AbstractArray{T} , dims:: DimOrInd... ) where {T} = similar (a, T, dims)
824+ similar (a:: AbstractArray , :: Type{T} , dims:: DimOrInd... ) where {T} = similar (a, T, dims)
825825# Similar supports specifying dims as either Integers or AbstractUnitRanges or any mixed combination
826826# thereof. Ideally, we'd just convert Integers to OneTos and then call a canonical method with the axes,
827827# but we don't want to require all AbstractArray subtypes to dispatch on Base.OneTo. So instead we
828828# define this method to convert supported axes to Ints, with the expectation that an offset array
829829# package will define a method with dims::Tuple{Union{Integer, UnitRange}, Vararg{Union{Integer, UnitRange}}}
830+ similar (a:: AbstractArray , :: Type{T} , dims:: Tuple{Union{Integer, AbstractOneTo}, Vararg{Union{Integer, AbstractOneTo}}} ) where {T} = similar (a, T, to_shape (dims))
831+ # legacy method for packages that specialize similar(A::AbstractArray, ::Type{T}, dims::Tuple{Union{Integer, OneTo, CustomAxis}, Vararg{Union{Integer, OneTo, CustomAxis}}}
832+ # leaving this method in ensures that Base owns the more specific method
830833similar (a:: AbstractArray , :: Type{T} , dims:: Tuple{Union{Integer, OneTo}, Vararg{Union{Integer, OneTo}}} ) where {T} = similar (a, T, to_shape (dims))
831834# similar creates an Array by default
832835similar (a:: AbstractArray , :: Type{T} , dims:: Dims{N} ) where {T,N} = Array {T,N} (undef, dims)
@@ -837,7 +840,7 @@ to_shape(dims::DimsOrInds) = map(to_shape, dims)::DimsOrInds
837840# each dimension
838841to_shape (i:: Int ) = i
839842to_shape (i:: Integer ) = Int (i)
840- to_shape (r:: OneTo ) = Int (last (r))
843+ to_shape (r:: AbstractOneTo ) = Int (last (r))
841844to_shape (r:: AbstractUnitRange ) = r
842845
843846"""
@@ -863,6 +866,8 @@ would create a 1-dimensional logical array whose indices match those
863866of the columns of `A`.
864867"""
865868similar (:: Type{T} , dims:: DimOrInd... ) where {T<: AbstractArray } = similar (T, dims)
869+ similar (:: Type{T} , shape:: Tuple{Union{Integer, AbstractOneTo}, Vararg{Union{Integer, AbstractOneTo}}} ) where {T<: AbstractArray } = similar (T, to_shape (shape))
870+ # legacy method for packages that specialize similar(::Type{T}, dims::Tuple{Union{Integer, OneTo, CustomAxis}, Vararg{Union{Integer, OneTo, CustomAxis}})
866871similar (:: Type{T} , shape:: Tuple{Union{Integer, OneTo}, Vararg{Union{Integer, OneTo}}} ) where {T<: AbstractArray } = similar (T, to_shape (shape))
867872similar (:: Type{T} , dims:: Dims ) where {T<: AbstractArray } = T (undef, dims)
868873
0 commit comments