Skip to content

Commit

Permalink
Make broadcast treat all type arguments as scalars.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sacha0 committed Jan 7, 2017
1 parent 28a11ff commit fcb22ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
13 changes: 7 additions & 6 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ function broadcast!{T,S,N}(::typeof(identity), x::AbstractArray{T,N}, y::Abstrac
end

# logic for deciding the resulting container type
containertype(x) = containertype(typeof(x))
containertype(x) = _containertype(typeof(x))
containertype(::Type) = Any
containertype{T<:Ptr}(::Type{T}) = Any
containertype{T<:Tuple}(::Type{T}) = Tuple
containertype{T<:Ref}(::Type{T}) = Array
containertype{T<:AbstractArray}(::Type{T}) = Array
containertype{T<:Nullable}(::Type{T}) = Nullable
_containertype(::Type) = Any
_containertype{T<:Ptr}(::Type{T}) = Any
_containertype{T<:Tuple}(::Type{T}) = Tuple
_containertype{T<:Ref}(::Type{T}) = Array
_containertype{T<:AbstractArray}(::Type{T}) = Array
_containertype{T<:Nullable}(::Type{T}) = Nullable
containertype(ct1, ct2) = promote_containertype(containertype(ct1), containertype(ct2))
@inline containertype(ct1, ct2, cts...) = promote_containertype(containertype(ct1), containertype(ct2, cts...))

Expand Down
10 changes: 9 additions & 1 deletion test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ end
Base.getindex(A::Array19745, i::Integer...) = A.data[i...]
Base.size(A::Array19745) = size(A.data)

Base.Broadcast.containertype{T<:Array19745}(::Type{T}) = Array19745
Base.Broadcast._containertype{T<:Array19745}(::Type{T}) = Array19745

Base.Broadcast.promote_containertype(::Type{Array19745}, ::Type{Array19745}) = Array19745
Base.Broadcast.promote_containertype(::Type{Array19745}, ::Type{Array}) = Array19745
Expand Down Expand Up @@ -436,3 +436,11 @@ end
@test f.([true, false]) == [true, "false"]
end
end

# Test that broadcast treats type arguments as scalars, i.e. containertype yields Any,
# even for subtypes of abstract array. (https://github.com/JuliaStats/DataArrays.jl/issues/229)
let
@test Base.Broadcast.containertype(AbstractArray) == Any
@test broadcast(==, [1], AbstractArray) == BitArray([false])
@test broadcast(==, 1, AbstractArray) == false
end

0 comments on commit fcb22ef

Please sign in to comment.