I define the following function, which seems pretty robust to me
myf(vs::Vector{N}) where {N<:Number} = [typeof(v) for v in vs]
But, using JET, I get a possible error
julia> JET.report_call(myf)
═════ 1 possible error found ═════
┌ myf(vs::Vector{N}) where N<:Number @ Main ./REPL[1]:1
│┌ collect(itr::Base.Generator{_A, var"#3#4"} where _A) @ Base ./array.jl:792
││┌ collect_to_with_first!(dest::Any, v1::DataType, itr::Base.Generator{_A, var"#3#4"} where _A, st::Any) @ Base ./array.jl:823
│││┌ grow_to!(dest::AbstractDict{K, V}, itr::Base.Generator{_A, var"#3#4"} where _A, st::Any) where {K, V} @ Base ./dict.jl:133
││││┌ indexed_iterate(I::DataType, i::Int64) @ Base ./tuple.jl:91
│││││ no matching method found `iterate(::DataType)`: x = iterate(I::DataType)
││││└────────────────────
I miss the fantasy to imagine how could this error be triggered by a user.
Any help ?
Investigating this a bit further I realize I could reproduce the JET possible error with
report_call(collect, Tuple{Base.Generator{N, typeof(typeof)} where N})
which is never going to happen, because instead I should always get
report_call(collect, Tuple{Base.Generator{Vector{N}, typeof(typeof)} where N<:Number})
,which successfully doesn’t report any possible errors.
So, I begin to think that the possible error happens because the compiler cannot be sure that the Generator will be a Generator{Vector}
and not Generator{Any}
.
But it seems pretty obvious to me. Am I missing something ?
Also, if I define the function as follows
myf2(vs::Vector{N}) where {N<:Number} = DataType[typeof(v) for v in vs]
, then report_call(myf2)
gives no possible errors.
So, is the problem that julia can’t infer that the elements type of a Vector{<:Number}
are DataType
s ?