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

#1

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

#2

Yes, it can, see e.g. https://julialang.org/blog/2018/08/union-splitting.

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

3 Likes
#3

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