What is the best way to help Julia’s compiler infer that the output type of the following function can only be Union{Int64, Nothing}
?
function inferme(x::Dict{String, Any}, y::String)
if haskey(x, y)
return Int(x[y])
else
return nothing
end
end
Currently Base.return_types(inferme, (Dict{String, Any}, String)) = Any
, which make sense (I think?) because there is no guarantee that x[y]
yields something that doesn’t break Int(...)
. But how can I convince the compiler that it should just trust me? (In reality x
is read from a JSON file, and its quite heterogenous, but the keys y
that I will feed into this function will have integers)
Context: I am attempting to implement an interface where the required functions have their output types also specified (and the interface tests against this). I already know about RequiredInterfaces.jl .