Is Julia able to optimize functions that can either return one specific type or `nothing`?


I know type stability of functions is a big factor in Julia performance, but I also Julia can also optimize Union{T,Nothing} where T in a lot of cases. Does this extend to functions without declared return types? I know that Julia can be efficient about some other kinds of Unions as well, and I’m curious when these optimizations can be applied (relative to type stability of functions and elsewhere).



Yes, it can, see e.g.

Yes, “declared return types” just inserts convert statements in the places the function can return.


Thanks! I’m sure I’ve read this post before, but sometimes I’m a slow learner!