Consider the issue of annotating types of objects taken from unknown sources. The following are two solutions to this problem:
function test1(a::Vector{Any})
x = a[1]::Int
2*x
end
and
function test2(a::Vector{Any})
x = convert(Int, a[1])
2*x
end
The manual recommends the first solution in the Performance-Tips section. It seems to me that the second solution might be preferable in many instances because the resulting code has greater genericity. But @code_warntype
on test2
gave me many more warnings than on test1
– it reported that x
and 2*x
in test2
were both of type Any
. (Julia 0.5.2).
Why does the compiler not know the types in test2
?