Skip to content

Moshi.jl

Algebraic data types, pattern matching, and trait derivation for Julia.

模式 · Pattern matching for Julia

Moshi.jl

Type-stable ADTs, exhaustive pattern matching, and Rust-style derives — built for modern Julia.

example.jl

using Moshi.Data: @data
using Moshi.Match: @match
@data Shape begin
Circle(Float64)
Rect(Float64, Float64)
end
area = @match shape begin
Shape.Circle(r) => π * r^2
Shape.Rect(w, h) => w * h
end

Three macros, one toolkit

Moshi (模式, móshì) brings ML-family patterns to Julia: sum types with @data, expressive @match, and @derive for common traits. It builds on ideas from MLStyle, SumTypes, and Expronicon.

using Moshi.Data: @data
using Moshi.Match: @match
using Moshi.Derive: @derive
@data Expr begin
Num(Int)
Add(Self, Self)
Mul(Self, Self)
end
@derive Expr[Show, Hash, Eq]
eval = @match expr begin
Expr.Num(n) => n
Expr.Add(a, b) => eval(a) + eval(b)
Expr.Mul(a, b) => eval(a) * eval(b)
end
pkg> add Moshi

Then head to the Quick Start guide or browse the API reference.