Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate similar(f, …) in favor of just dispatching directly on f(…) #26733

Merged
merged 5 commits into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove AbstractArray constructors; just use dispatch on functions
  • Loading branch information
mbauman committed Apr 6, 2018
commit 982b1a74c8072c91d95385f4f79250746e322484
35 changes: 6 additions & 29 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,12 @@ convert(::Type{T}, a::T) where {T<:AbstractArray} = a
convert(::Type{AbstractArray{T}}, a::AbstractArray) where {T} = AbstractArray{T}(a)
convert(::Type{AbstractArray{T,N}}, a::AbstractArray{<:Any,N}) where {T,N} = AbstractArray{T,N}(a)

to_axis(d::Integer) = OneTo(d)
to_axis(d::AbstractUnitRange) = d

# AbstractArray constructors: Gather args into a `AbstractArray{T,N}(undef, ::Tuple)` method
if nameof(@__MODULE__) === Core # avoid method overwrite with Core
# These methods and the types in these methods are shared between Core and Base
AbstractArray{T}(::UndefInitializer, dims::Vararg{Integer,N}) where {T,N} = AbstractArray{T,N}(undef, dims)
AbstractArray{T}(::UndefInitializer, dims::NTuple{N,Integer}) where {T,N} = AbstractArray{T,N}(undef, dims)

AbstractArray{T,N}(::UndefInitializer, dims::NTuple{N,Integer}) where {T,N} = Array{T,N}(undef, dims)
AbstractArray{Bool,N}(::UndefInitializer, dims::NTuple{N,Integer}) where {N} = BitArray{N}(undef, dims...)
elseif nameof(@__MODULE__) === :Base # avoid method overwrite with Core
# Add definitions supporting axes in Base alone — Core.OneTo is not shared with Base.OneTo
AbstractArray{T}(::UndefInitializer, dims::Vararg{DimOrInd,N}) where {T,N} = AbstractArray{T,N}(undef, dims)
AbstractArray{T}(::UndefInitializer, dims::NTuple{N,DimOrInd}) where {T,N} = AbstractArray{T,N}(undef, dims)
AbstractArray{T,N}(::UndefInitializer, dims::Vararg{DimOrInd,N}) where {T,N} = AbstractArray{T,N}(undef, dims)

AbstractArray{T,N}(::UndefInitializer, dims::NTuple{N,Union{Integer, OneTo}}) where {T,N} = AbstractArray{T,N}(undef, map(to_axis, dims))

AbstractArray{T,N}(::UndefInitializer, dims::NTuple{N,OneTo}) where {T,N} = Array{T,N}(undef, map(last, dims))
AbstractArray{T,0}(::UndefInitializer, ::Tuple{}) where {T} = Array{T,0}(undef)
AbstractArray{Bool,N}(::UndefInitializer, dims::NTuple{N,OneTo}) where {N} = BitArray{N}(undef, map(last, dims)...)
AbstractArray{Bool,0}(::UndefInitializer, ::Tuple{}) where {} = BitArray{0}(undef)

# catch undefined constructors before the deprecation kicks in
# TODO: remove when deprecation is removed
function (::Type{T})(arg) where {T<:AbstractArray}
throw(MethodError(T, (arg,)))
end
if nameof(@__MODULE__) === :Base # avoid method overwrite
# catch undefined constructors before the deprecation kicks in
# TODO: remove when deprecation is removed
function (::Type{T})(arg) where {T<:AbstractArray}
throw(MethodError(T, (arg,)))
end
end

"""
Expand Down
18 changes: 11 additions & 7 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ function fill!(a::Array{T}, x) where T<:Union{Integer,AbstractFloat}
return a
end

to_dim(d::Integer) = d
to_dim(d::OneTo) = last(d)

"""
fill(x, dims)

Expand All @@ -347,11 +350,10 @@ julia> fill(1.0, (5,5))
If `x` is an object reference, all elements will refer to the same object. `fill(Foo(),
dims)` will return an array filled with the result of evaluating `Foo()` once.
"""
fill(v, dims::NTuple{N, Integer}) where {N} = fill!(Array{typeof(v), N}(undef, dims), v)
fill(v, dims::NTuple{N, OneTo}) where {N} = fill!(Array{typeof(v), N}(undef, map(last, dims)), v)
fill(v, dims::NTuple{N, Union{Integer, OneTo}}) where {N} = fill!(Array{typeof(v), N}(undef, map(to_axis, dims)), v)
fill(v, dims::Tuple{}) = fill!(Array{typeof(v), 0}(undef), v)
fill(v, dims::DimOrInd...) = fill(v, dims)
fill(v, dims::NTuple{N, Union{Integer, OneTo}}) where {N} = fill(v, map(to_dim, dims))
fill(v, dims::NTuple{N, Integer}) where {N} = fill!(Array{typeof(v),N}(undef, dims), v)
fill(v, dims::Tuple{}) = fill!(Array{typeof(v),0}(undef, dims), v)

"""
zeros([T=Float64,] dims...)
Expand Down Expand Up @@ -395,10 +397,12 @@ function ones end

for (fname, felt) in ((:zeros, :zero), (:ones, :one))
@eval begin
$fname(::Type{T}, dims::NTuple{N,DimOrInd}) where {T,N} = fill!(AbstractArray{T,N}(undef, dims), $felt(T))
$fname(dims::Tuple{Vararg{DimOrInd}}) = $fname(Float64, dims)
$fname(::Type{T}, dims::DimOrInd...) where {T} = $fname(T, dims)
$fname(dims::DimOrInd...) = $fname(dims)
$fname(::Type{T}, dims::DimOrInd...) where {T} = $fname(T, dims)
$fname(dims::Tuple{Vararg{DimOrInd}}) = $fname(Float64, dims)
$fname(::Type{T}, dims::NTuple{N, Union{Integer, OneTo}}) where {T,N} = $fname(T, map(to_dim, dims))
$fname(::Type{T}, dims::NTuple{N, Integer}) where {T,N} = fill!(Array{T,N}(undef, map(to_dim, dims)), $felt(T))
$fname(::Type{T}, dims::Tuple{}) where {T} = fill!(Array{T}(undef), $felt(T))
end
end

Expand Down
12 changes: 8 additions & 4 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ julia> BitArray(undef, (3, 1))
"""
BitArray(::UndefInitializer, dims::Integer...) = BitArray(undef, map(Int,dims))
BitArray{N}(::UndefInitializer, dims::Integer...) where {N} = BitArray{N}(undef, map(Int,dims))
BitArray(::UndefInitializer, dims::NTuple{N,Int}) where {N} = BitArray{N}(undef, dims...)
BitArray{N}(::UndefInitializer, dims::NTuple{N,Int}) where {N} = BitArray{N}(undef, dims...)
BitArray(::UndefInitializer, dims::NTuple{N,Integer}) where {N} = BitArray{N}(undef, map(Int, dims)...)
BitArray{N}(::UndefInitializer, dims::NTuple{N,Integer}) where {N} = BitArray{N}(undef, map(Int, dims)...)

const BitVector = BitArray{1}
const BitMatrix = BitArray{2}
Expand Down Expand Up @@ -366,8 +366,10 @@ julia> falses(2,3)
false false false
```
"""
falses(dims::Tuple{Vararg{DimOrInd}}) = fill!(AbstractArray{Bool}(undef, dims), false)
falses(dims::DimOrInd...) = falses(dims)
falses(dims::NTuple{N, Union{Integer, OneTo}}) where {N} = falses(map(to_dim, dims))
falses(dims::NTuple{N, Integer}) where {N} = fill!(BitArray(undef, dims), false)
falses(dims::Tuple{}) = fill!(BitArray(undef, dims), false)

"""
trues(dims)
Expand All @@ -382,8 +384,10 @@ julia> trues(2,3)
true true true
```
"""
trues(dims::Tuple{Vararg{DimOrInd}}) = fill!(AbstractArray{Bool}(undef, dims), true)
trues(dims::DimOrInd...) = trues(dims)
trues(dims::NTuple{N, Union{Integer, OneTo}}) where {N} = trues(map(to_dim, dims))
trues(dims::NTuple{N, Integer}) where {N} = fill!(BitArray(undef, dims), true)
trues(dims::Tuple{}) = fill!(BitArray(undef, dims), true)

function one(x::BitMatrix)
m, n = size(x)
Expand Down
23 changes: 15 additions & 8 deletions test/TestHelpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,18 @@ Base.similar(::Type{T}, shape::Tuple{UnitRange,Vararg{UnitRange}}) where {T<:Arr
Base.similar(::Type{T}, shape::Tuple{UnitRange,Vararg{UnitRange}}) where {T<:BitArray} =
OffsetArray(T(undef, map(length, shape)), map(indsoffset, shape))

Base.reshape(A::AbstractArray, inds::Tuple{UnitRange,Vararg{UnitRange}}) = OffsetArray(reshape(A, map(length, inds)), map(indsoffset, inds))

Base.fill(v, inds::NTuple{N, AbstractUnitRange}) where{N} =
fill!(OffsetArray(Array{typeof(v), N}(undef, map(length, inds)), map(indsoffset, inds)), v)
Base.AbstractArray{T}(::UndefInitializer, inds::NTuple{N,AbstractUnitRange}) where {T,N} =
OffsetArray(Array{T, N}(undef, map(length, inds)), map(indsoffset, inds))
Base.AbstractArray{T,N}(::UndefInitializer, inds::NTuple{N,AbstractUnitRange}) where {T,N} =
OffsetArray(Array{T, N}(undef, map(length, inds)), map(indsoffset, inds))
Base.reshape(A::AbstractArray, inds::Tuple{UnitRange,Vararg{UnitRange}}) = OffsetArray(reshape(A, map(indslength, inds)), map(indsoffset, inds))

Base.fill(v, inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {N} =
fill!(OffsetArray(Array{typeof(v), N}(undef, map(indslength, inds)), map(indsoffset, inds)), v)
Base.zeros(::Type{T}, inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {T, N} =
fill!(OffsetArray(Array{T, N}(undef, map(indslength, inds)), map(indsoffset, inds)), zero(T))
Base.ones(::Type{T}, inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {T, N} =
fill!(OffsetArray(Array{T, N}(undef, map(indslength, inds)), map(indsoffset, inds)), one(T))
Base.trues(inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {N} =
fill!(OffsetArray(BitArray{N}(undef, map(indslength, inds)), map(indsoffset, inds)), true)
Base.falses(inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {N} =
fill!(OffsetArray(BitArray{N}(undef, map(indslength, inds)), map(indsoffset, inds)), false)

@inline function Base.getindex(A::OffsetArray{T,N}, I::Vararg{Int,N}) where {T,N}
checkbounds(A, I...)
Expand Down Expand Up @@ -167,6 +171,9 @@ _offset(out, ::Tuple{}, ::Tuple{}) = out

indsoffset(r::AbstractRange) = first(r) - 1
indsoffset(i::Integer) = 0
indslength(r::AbstractRange) = length(r)
indslength(i::Integer) = i


Base.resize!(A::OffsetVector, nl::Integer) = (resize!(A.parent, nl); A)

Expand Down
2 changes: 1 addition & 1 deletion test/compiler/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ end

# issue #5575
f5575() = zeros(Type[Float64][1], 1)
@test Base.return_types(f5575, ())[1] == Union{Vector, BitVector}
@test Base.return_types(f5575, ())[1] == Vector

# make sure Tuple{unknown} handles the possibility that `unknown` is a Vararg
function maybe_vararg_tuple_1()
Expand Down