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

Fix eltype of flatten of tuple with non-2 length #56802

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 19 additions & 2 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ using .Base:
AbstractRange, AbstractUnitRange, UnitRange, LinearIndices, TupleOrBottom,
(:), |, +, -, *, !==, !, ==, !=, <=, <, >, >=, =>, missing,
any, _counttuple, eachindex, ntuple, zero, prod, reduce, in, firstindex, lastindex,
tail, fieldtypes, min, max, minimum, zero, oneunit, promote, promote_shape, LazyString
tail, fieldtypes, min, max, minimum, zero, oneunit, promote, promote_shape, LazyString,
tuple_type_tail
using Core: @doc

using .Base:
Expand Down Expand Up @@ -1202,7 +1203,23 @@ julia> [(x,y) for x in 0:1 for y in 'a':'c'] # collects generators involving It
flatten(itr) = Flatten(itr)

eltype(::Type{Flatten{I}}) where {I} = eltype(eltype(I))
eltype(::Type{Flatten{I}}) where {I<:Union{Tuple,NamedTuple}} = promote_typejoin(map(eltype, fieldtypes(I))...)

# For tuples, we statically know the element type of each index, so we can compute
# this at compile time.
eltype(::Type{Flatten{I}}) where {I<:Tuple} = _flatten_eltype(Union{}, I)

function eltype(::Type{Flatten{I}}) where {I<:NamedTuple{<:Any, T}} where T
_flatten_eltype(Union{}, T)
end

function _flatten_eltype(T::Type, I::Type{<:Tuple{E, Vararg{Any}}}) where E
T2 = promote_typejoin(T, eltype(E))
T2 === Any && return Any
_flatten_eltype(T2, tuple_type_tail(I))
end

_flatten_eltype(T::Type, I::Type{Tuple{}}) = T

eltype(::Type{Flatten{Tuple{}}}) = eltype(Tuple{})
IteratorEltype(::Type{Flatten{I}}) where {I} = _flatteneltype(I, IteratorEltype(I))
IteratorEltype(::Type{Flatten{Tuple{}}}) = IteratorEltype(Tuple{})
Expand Down
3 changes: 3 additions & 0 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,9 @@ end
@test eltype(flatten(UnitRange{Int8}[1:2, 3:4])) == Int8
@test eltype(flatten(([1, 2], [3.0, 4.0]))) == Real
@test eltype(flatten((a = [1, 2], b = Int8[3, 4]))) == Signed
@test eltype(flatten((Int[], Nothing[], Int[]))) == Union{Int, Nothing}
@test eltype(flatten((String[],))) == String
@test eltype(flatten((Int[], UInt[], Int8[],))) == Integer
@test length(flatten(zip(1:3, 4:6))) == 6
@test length(flatten(1:6)) == 6
@test collect(flatten(Any[])) == Any[]
Expand Down
Loading