Sukera already provided an example of a method where one call is stable and another call is unstable, so if you’re fine with that inconsistency between calls, then attaching an automatic warning feature to a method would be fine for you. It’ll just be complicated to implement, especially throwing an error during type inference of a simple foo(3)
call, and I think inspecting type stability per function call would be more useful for compiling small executables and just generally. At least we’re on the same page on what this could accomplish, now.
The type of x[1]
is not inferrable. All variables’ runtime values have concrete types, that doesn’t imply all variables or expressions have inferrable types (inference occurs at compile-time, not runtime):
julia> g(Any[0])
0
julia> isconcretetype(typeof(Any[0]))
true
julia> isconcretetype(typeof(0))
true
julia> @code_warntype g(Any[0])
MethodInstance for g(::Vector{Any})
from g(x::Vector{Any}) in Main at REPL[27]:1
Arguments
#self#::Core.Const(g)
x::Vector{Any}
Body::Any
1 ─ %1 = Base.getindex(x, 1)::Any
└── return %1