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

RFC: Use typejoin of the field types for eltype of heterogeneous Tuples #21108

Merged
merged 3 commits into from
Mar 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
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))
t´ = unwrap_unionall(t)
r = Union{}
for ti in t´.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