Skip to content

Commit

Permalink
Additionally fix #198 by using Base.promote_op
Browse files Browse the repository at this point in the history
  • Loading branch information
wsshin committed Aug 8, 2017
1 parent 3a49b3a commit e45d995
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ end
end

eltype_exprs = [t <: AbstractArray ? :($(eltype(t))) : :($t) for t a]
newtype_expr = :(Core.Inference.return_type(f, Tuple{$(eltype_exprs...)}))
newtype_expr = prod(newsize) > 0 ? :(Base.promote_op(f, $(eltype_exprs...))) : :(Union{})

return quote
@_inline_meta
Expand Down
2 changes: 1 addition & 1 deletion src/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ end
exprs[i] = :(f($(tmp...)))
end
eltypes = [eltype(a[j]) for j 1:length(a)] # presumably, `eltype` is "hyperpure"?
newT = :(Core.Inference.return_type(f, Tuple{$(eltypes...)}))
newT = :(Base.promote_op(f, $(eltypes...)))
return quote
@_inline_meta
@inbounds return similar_type(typeof(_first(a...)), $newT, Size(S))(tuple($(exprs...)))
Expand Down
24 changes: 12 additions & 12 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,22 @@ end
@testset "eltype after broadcast" begin
# test cases issue #198
let a = SVector{4, Number}(2, 2.0, 4//2, 2+0im)
@test_broken eltype(a + 2) == Number
@test_broken eltype(a - 2) == Number
@test_broken eltype(a * 2) == Number
@test_broken eltype(a / 2) == Number
@test eltype(a + 2) == Number
@test eltype(a - 2) == Number
@test eltype(a * 2) == Number
@test eltype(a / 2) == Number
end
let a = SVector{3, Real}(2, 2.0, 4//2)
@test_broken eltype(a + 2) == Real
@test_broken eltype(a - 2) == Real
@test_broken eltype(a * 2) == Real
@test_broken eltype(a / 2) == Real
@test eltype(a + 2) == Real
@test eltype(a - 2) == Real
@test eltype(a * 2) == Real
@test eltype(a / 2) == Real
end
let a = SVector{3, Real}(2, 2.0, 4//2)
@test_broken eltype(a + 2.0) == Float64
@test_broken eltype(a - 2.0) == Float64
@test_broken eltype(a * 2.0) == Float64
@test_broken eltype(a / 2.0) == Float64
@test eltype(a + 2.0) == Real
@test eltype(a - 2.0) == Real
@test eltype(a * 2.0) == Real
@test eltype(a / 2.0) == Real
end
let a = broadcast(Float32, SVector(3, 4, 5))
@test eltype(a) == Float32
Expand Down

0 comments on commit e45d995

Please sign in to comment.