I was wondering if anyone could explain why changing the type of a variable is problematic in Julia.
I (think) I understand the reason type-instabilities are a problem when they arise conditional on the value (rather than type) of input arguments. When that happens, as I understand it, it causes problems for the compiler because the compiler is only conditioning its code on function input types, not input values, so it can’t make a clean type inference. e.g.
function x(a::Integer) if a % 2 == 0 return a else return "a is odd" end
Is type unstable because if a is even , the function returns an integer, if its odd it return a string. And the compiler doesn’t condition on the value of a, just it’s type (here, Integer).
But it’s much less clear to me why changing variables types, as in this example from the Julia docs:
function foo() x = 1 for i = 1:10 x = x/bar() end return x end
is problematic, since it seems like the changes in variable type are fully deterministic and thus, I would have thought, fully transparent to the compiler.
Could anyone explain why that’s something the compiler can’t handle?