Refine eltype(::Type{<:Flatten})#48276
Closed
jakobnissen wants to merge 1 commit intoJuliaLang:masterfrom
Closed
Conversation
The new approach uses `typejoin` over `eltype` of all the underlying iterators of the `Flatten`. This way, `eltype(flatten(((1,), [1]))) == Int`, whereas it was `Any` before, due to the heterogenous underlying iterators.
gaurav-arya
reviewed
Jan 14, 2023
Comment on lines
+1170
to
+1174
| tuple_eltype_join(::Type{Tuple{T}}) where T = eltype(T) | ||
| function tuple_eltype_join(::Type{T}) where {T <: Tuple} | ||
| E = eltype(Base.tuple_type_head(T)) | ||
| E === Any ? Any : typejoin(E, tuple_eltype_join(Base.tuple_type_tail(T))) | ||
| end |
Contributor
There was a problem hiding this comment.
Suggested change
| tuple_eltype_join(::Type{Tuple{T}}) where T = eltype(T) | |
| function tuple_eltype_join(::Type{T}) where {T <: Tuple} | |
| E = eltype(Base.tuple_type_head(T)) | |
| E === Any ? Any : typejoin(E, tuple_eltype_join(Base.tuple_type_tail(T))) | |
| end | |
| tuple_eltype_join(::Type{T}) where {T <: Tuple} = Base.typejoin(eltype.(fieldtypes(T))...) |
Does the above also work, or are there reasons to use the recursive implementation? I suppose the recursive implementation might terminate faster if the typejoin becomes Any?
Contributor
There was a problem hiding this comment.
(Superceded by next suggestion)
gaurav-arya
reviewed
Jan 14, 2023
Comment on lines
+1168
to
+1174
| eltype(::Type{Base.Iterators.Flatten{T}}) where T = tuple_eltype_join(T) | ||
|
|
||
| tuple_eltype_join(::Type{Tuple{T}}) where T = eltype(T) | ||
| function tuple_eltype_join(::Type{T}) where {T <: Tuple} | ||
| E = eltype(Base.tuple_type_head(T)) | ||
| E === Any ? Any : typejoin(E, tuple_eltype_join(Base.tuple_type_tail(T))) | ||
| end |
Contributor
There was a problem hiding this comment.
Line 397 in 1c449b3
Suggested change
| eltype(::Type{Base.Iterators.Flatten{T}}) where T = tuple_eltype_join(T) | |
| tuple_eltype_join(::Type{Tuple{T}}) where T = eltype(T) | |
| function tuple_eltype_join(::Type{T}) where {T <: Tuple} | |
| E = eltype(Base.tuple_type_head(T)) | |
| E === Any ? Any : typejoin(E, tuple_eltype_join(Base.tuple_type_tail(T))) | |
| end | |
| eltype(::Type{Base.Iterators.Flatten{T}}) where T = Base.typejoin(map(eltype, fieldtypes(T))...) |
Contributor
|
Also, in any case, we still need to handle the case where the It might also be nice to fix the Assuming these are valid issues, happy to take a stab at patching them. |
Member
Author
|
Closing in favor of #48277 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The new approach uses
typejoinovereltypeof all the underlying iterators of theFlatten. This way,eltype(flatten(((1,), [1]))) == Int, whereas it wasAnybefore, due to the heterogenous underlying iterators.Closes #48249