Here's me copying your second example as closely as possible in julia:
using MLStyle
mutable struct TypeStable
o::Union{Some{Int}, Nothing}
end
MLStyle.@as_record TypeStable
MLStyle.@as_record Nothing
function type_stable()
o = returns_option()
@match o.o begin
Some(_) => 1
Nothing() => 0
end
end
returns_option() = TypeStable(rand(Bool) ? Some(rand(1:10)) : nothing)
And what does the compiler have to say about it? julia> Core.Compiler.return_type(type_stable, Tuple{})
Int64
and julia> @code_warntype type_stable()
Variables
#self#::Core.Const(type_stable)
o::TypeStable
259::Union{Nothing, Some{Int64}}
return#257::Union{Nothing, Int64}
Body::Int64
1 ─ (o = Main.returns_option())
│ (return#257 = Main.nothing)
│ (259 = Base.getproperty(o, :o))
│ %4 = (259 isa Nothing)::Bool
└── goto #3 if not %4
2 ─ (return#257 = 0)
└── goto #5
3 ─ %8 = (259::Some{Int64} isa Some)::Core.Const(true)
│ %8
│ %10 = (259::Some{Int64} !== Main.nothing)::Core.Const(true)
│ %10
│ (return#257 = 1)
└── goto #5
4 ─ Core.Const(:((error)("matching non-exhaustive, at #= REPL[9]:3 =#")))
5 ┄ return return#257::Int64
This is on version 1.6.1 for me, but should work fine on earlier releases. I agree that having MLStyle.jl pattern matching bundled into julia would be great!