Skip to content

Commit

Permalink
Merge pull request #21108 from JuliaLang/mh/tuple_eltype
Browse files Browse the repository at this point in the history
RFC: Use `typejoin` of the field types for `eltype` of heterogeneous `Tuple`s
  • Loading branch information
JeffBezanson authored Mar 23, 2017
2 parents 56bfb23 + 4752d24 commit d694548
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 10 additions & 0 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ first(t::Tuple) = t[1]

eltype(::Type{Tuple{}}) = Bottom
eltype(::Type{<:Tuple{Vararg{E}}}) where {E} = E
function eltype(t::Type{<:Tuple})
@_pure_meta
t isa Union && return typejoin(eltype(t.a), eltype(t.b))
= unwrap_unionall(t)
r = Union{}
for ti in.parameters
r = typejoin(r, rewrap_unionall(unwrapva(ti), t))
end
return r
end

# version of tail that doesn't throw on empty tuples (used in array indexing)
safe_tail(t::Tuple) = tail(t)
Expand Down
8 changes: 7 additions & 1 deletion test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,14 @@ end
@test eltype((1,2,3)) === Int
@test eltype((1.0,2.0,3.0)) <: AbstractFloat
@test eltype((true, false)) === Bool
@test eltype((1,2.0, false)) === Any
@test eltype((1, 2.0, false)) === typejoin(Int, Float64, Bool)
@test eltype(()) === Union{}
@test eltype(Tuple{Int, Float64, Vararg{Bool}}) === typejoin(Int, Float64, Bool)
@test eltype(Tuple{Int, T, Vararg{Bool}} where T <: AbstractFloat) ===
typejoin(Int, AbstractFloat, Bool)
@test eltype(Tuple{Int, Bool, Vararg{T}} where T <: AbstractFloat) ===
typejoin(Int, AbstractFloat, Bool)
@test eltype(Union{Tuple{Int, Float64}, Tuple{Vararg{Bool}}}) === typejoin(Int, Float64, Bool)

begin
local foo
Expand Down

0 comments on commit d694548

Please sign in to comment.