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).

Thanks

Yes, it can, see e.g. Union-splitting: what it is, and why you should care.

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

3 Likes

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