How to resolve `Any`s in this function?

The reason you get Union for this:

function oc(g,a,P)
    if g == 1
        ret = P[g,a]
    elseif g == 2
        ret = P[g,a] + 1
    end
    return ret
end

is quite simple: what should ret be, if g is neither 1 nor 2? The compiler doesn’t pull a value out of thin air, and since no value was specified, that case is inferred as Nothing. Together with the other branches, the complete return type becomes Union{Nothing, Int}, to signal that the returned value is either an Int or nothing.

2 Likes