What is the Julia equivalent of Rust Option<T>?

I found this old blog post that does a better job of differentiating Julia’s Union and Rust’s sum types. The sections on the practical pros and cons for either approach are of particular interest. Link: Union vs sum types

I would guess that the only reason sum types aren’t utilized as much in Julia is that type optimization tends to go to extremes. Ideally you have perfect type inference in a generic method; you might get nothing and missing in some contexts, but those don’t escalate to more types (operations on missing propagate to missing, ones on nothing error) and are quickly handled. People don’t tend to maintain custom small Unions through a method, and why would they when the compiler only tracks a few? When there are more, maybe even infinite, types the compiler cannot generally track, optimization then becomes about isolating the dynamic dispatches to small caller methods, and the JIT-compiled callees can be perfectly inferred again. The middle ground of telling the compiler the finite number of types you handle with more care is more necessary for a AOT-compiled language. AOT-compiled juliac is being developed for its lower overhead, and I predict that sum types will become far more appreciated when the JIT compiler and its flexibility isn’t around.

5 Likes