-
-
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
Create infer_eltype
function
#54909
Create infer_eltype
function
#54909
Conversation
There is an older discussion about exposing Julia's type inference as API with a mechanism like Base.result_type. This overlaps substantially |
I think you are referring to #54207, right? I would love to see an user-facing type inference API for sure, at the same time I think a function like this one could be also part of that API so maybe a PR which addresses just that could be accepted nonetheless (maybe without exporting the function at first) |
I really regret having any form of |
To elaborate on Jeff's point: adding a new function that is like |
I also considered to use inference by default, but some comments (cc. @nsajko) made me realize that inference is not totally reliable and so maybe having the user to acknowledge that with a new function like |
The problem is not it being "unreliable", but that (1) we want to be able to change it between versions (e.g. to trade off precision and compile times), and (2) it's nice to be able to run programs in an interpreter and get the same results. The results also depend on arbitrary heuristics like how many Union elements we want to allow. |
From traige: Nice job on the implementation, but from a language design perspective, we want to move away from relying on inference for semantics. |
thanks! I hope there is then a solution which doesn't involve inference. Is there? |
There is are solutions which don't involve inference. Though they don't infer the type for you and instead you have to explicitly declare it. For example: julia> struct EltypeIter{T,P}
parent::P
end
julia> EltypeIter{T}(x) where T = EltypeIter{T, typeof(x)}(x)
julia> Base.eltype(::EltypeIter{T}) where T = T
julia> Base.iterate(x::EltypeIter, i...) = iterate(x.parent, i...)
julia> Base.IteratorSize(::EltypeIter{T, P}) where {T, P} = Base.IteratorSize(P)
julia> Base.length(x::EltypeIter) = length(x.parent)
julia> Base.size(x::EltypeIter) = size(x.parent)
julia> struct A end
julia> struct B end
julia> itr = (x < 5 ? A() : B() for x in 1:10);
julia> eltype(itr)
Any
julia> itr2 = EltypeIter{Union{A, B}}(itr)
EltypeIter{Union{A, B}, Base.Generator{UnitRange{Int64}, var"#5#6"}}(Base.Generator{UnitRange{Int64}, var"#5#6"}(var"#5#6"(), 1:10))
julia> eltype(itr2)
Union{A, B}
julia> collect(itr)
10-element Vector{Any}:
A()
A()
A()
A()
B()
B()
B()
B()
B()
B()
julia> collect(itr2)
10-element Vector{Union{A, B}}:
A()
A()
A()
A()
B()
B()
B()
B()
B()
B() |
I'm becoming radicalized against In other words, instead of trying to get a smarter |
Addresses #54157
This PR adds a function which can be useful when one wants to infer the eltype of a collection with only a loose
eltype
but which could be made tighter by using type inference, for example: