Getting Started
Moshi (模式) is a Julia package for defining and working with algebraic data types (ADTs) and pattern matching. It also provides a derive macro similar to Rust’s derive macro for deriving traits (a set of interface functions) for ADTs or Julia structs.
The Name and Acknowledgement
The name “Moshi” is derived from the Chinese word “模式” (móshì) which means “pattern”. The design of pattern matching is inspired by its predecessor MLStyle, which tries to bring pattern matching and algebraic data types from ML family languages to Julia.
The generic algebraic data type is highly inspired by previous work done in Julia ecosystem:
- SumTypes by @MasonProtter.
- Expronicon by myself.
- this discussion about “generated struct” and how Julia implements
Union
types by @vjnash.
Installation
You can install Moshi
using the Julia package manager. From the Julia REPL, type ]
to enter the Pkg REPL mode and run:
Quick Example
Here is a quick example of defining a simple algebraic data type:
For pattern matching, if you already used MLStyle
, the syntax is very similar:
Further Reading
- Examples and detailed syntax for
@data
: ADT Syntax - Builtin patterns for
@match
: Builtin Patterns - The
@derive
macro: Syntax and Examples
To understand how Moshi works, you can check the following sections: