-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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 Tuple
s
#21108
Conversation
FWIW, I think this behavior is preferable. |
base/tuple.jl
Outdated
t´ = unwrap_unionall(t) | ||
r = Union{} | ||
for ti in t´.parameters | ||
r = typejoin(r, unwrapva(ti)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rewrap_unionall
should be done at this point, since typejoin
won't work on types with free variables (it calls <:
).
Just noticed that this presently errors for function eltype(u::Union)
typejoin(eltype(u.a), eltype(u.b))
end ? That would also make something like |
Actually, allowing |
Yes, but there's also a difference between a collection with element type Anyway, as said, I'm not sure of the usefulness of this myself, so no offense taken if the PR gets rejected. |
Addressed the review comment but will leave the |
Element type |
There isn't really any good way to program with such a result in user code. To support that, |
Hm, if we don't do this, we should probably make |
I think this change does make sense, at least for concrete tuple types. For example if we made heterogeneous tuples |
This fixes #20143. |
I think it is necessary to check that the tuple type is actually a julia> eltype(Union{Tuple{Int},Tuple{String}})
ERROR: type Union has no field parameters
Stacktrace:
[1] eltype(::Type{Union{Tuple{Int64}, Tuple{String}}}) at ./REPL[11]:5 |
Added recursive handling of julia> eltype(Union{Tuple{Int}, Tuple{Float64}})
Real
julia> eltype(Union{Vector{Int}, Vector{Float64}})
Any Would we rather have |
This is a bit related to some things I've been thinking about lately, and it came up again today. It's been noticed that since StaticArrays uses tuples in many places, we could remove remaining calls to It's even quite simple to allow inhomogenous tuples as static array storage: with a bit of effort we could even make methods for In this picture, eltype would be computed by something like this PR. It would have to be done by an outer constructor, to satisfy Anyway, suffice it to say that the behaviour discussed in this PR will effect those downstream working with inhomogenous collections and trying to mimic "covariantish" (for lack of a better term) behavior. Personally, I support using |
julia> reduce(Core.Inference.tmerge, (Int8, Int16, Int32))
Union{Int16, Int32, Int8}
julia> reduce(typejoin, (Int8, Int16, Int32))
Signed
julia> reduce(Core.Inference.tmerge, (Int8, Int16, Int32, Int64))
Any
julia> reduce(typejoin, (Int8, Int16, Int32, Int64))
Signed I don't think implementing a mechanism that always produces the more specific of these two as part of |
@andyferris See Line 446 in 90cfb82
That is the preferred way to write map without return_type . It checks the type of each element, calling typejoin as necessary, and if the types can be inferred then all the type-checking and promoting code gets specialized away.
|
I see it, thanks Jeff! I wondered if this might be the approach in If we were worried about this kind of slow down, one could iterate over (For the case of static arrays, the type join could be done as some pure or generated function over the data's tuple type, so it's relatively straightforward to elide). @martinholters Yes, good point. Changing |
The widening in inference is not done to come up with types that are best for codegen, but to ensure convergence of inference itself. And more aggressive widening allows faster convergence. That said, there is also the "TODO: something smarter, like a common supertype" comment in |
With the |
Came up here: #21077 (comment)
Not sure this is actually helpful anywhere, but it probably won't hurt either...
Edit: Closes #20143.