Skip to content

Commit

Permalink
Use promotion rather than typejoin to infer map/broadcast output eltype
Browse files Browse the repository at this point in the history
This allows `Union`s as the output type for mixtures of `Null` and
numeric types, which can be more efficient than the `Any` you'd get from
typejoin, at least in 0.7.

It also makes `SVectors` of abstract types like `Number` become concrete
after numerical operations, which is inconsistent with base, but
probably not a problem for realisitc uses of this package.
  • Loading branch information
c42f committed Nov 3, 2017
1 parent 4231a73 commit 0a78009
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ end
return quote
@_inline_meta
elements = tuple($(exprs...))
@inbounds return similar_type($first_staticarray, eltype(elements), Size($newsize))(elements)
@inbounds return similar_type($first_staticarray, promote_tuple_eltype(elements), Size($newsize))(elements)
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ end
return quote
@_inline_meta
elements = tuple($(exprs...))
@inbounds return similar_type(typeof(_first(a...)), eltype(elements), Size(S))(elements)
@inbounds return similar_type(typeof(_first(a...)), promote_tuple_eltype(elements), Size(S))(elements)
end
end

Expand Down Expand Up @@ -126,7 +126,7 @@ end
return quote
@_inline_meta
elements = tuple($(exprs...))
@inbounds return similar_type(a, eltype(elements), Size($Snew))(elements)
@inbounds return similar_type(a, promote_tuple_eltype(elements), Size($Snew))(elements)
end
end

Expand Down
16 changes: 8 additions & 8 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,16 @@ end
@testset "eltype after broadcast" begin
# test cases issue #198
let a = SVector{4, Number}(2, 2.0, 4//2, 2+0im)
@test eltype(a + 2) == Number
@test eltype(a - 2) == Number
@test eltype(a * 2) == Number
@test eltype(a / 2) == Number
@test_broken eltype(a + 2) == Number
@test_broken eltype(a - 2) == Number
@test_broken eltype(a * 2) == Number
@test_broken eltype(a / 2) == Number
end
let a = SVector{3, Real}(2, 2.0, 4//2)
@test eltype(a + 2) == Real
@test eltype(a - 2) == Real
@test eltype(a * 2) == Real
@test eltype(a / 2) == Real
@test_broken eltype(a + 2) == Real
@test_broken eltype(a - 2) == Real
@test_broken eltype(a * 2) == Real
@test_broken eltype(a / 2) == Real
end
let a = SVector{3, Real}(2, 2.0, 4//2)
@test eltype(a + 2.0) == Float64
Expand Down

0 comments on commit 0a78009

Please sign in to comment.