mason

patterns

Mason supports single-level pattern-matching inside of case expressions.

Point = class x y

magnitude = \case
	|| This is a simple expression.
	:Num
		_
	|| Write extracted variables from the type.
	:Point x y
		Math.sqrt + (** x 2) ** y 2

. magnitude 0
. magnitude new Point 3 4

You can write custom pattern matching by implementing the builtin extract method.

This should return null or an array of extracted values.

multiple-of = \n
	with {}
		self-impl! extract _ \case
			divisible? _ n
				[/ _ n]
			else
				null

foo = \case
	:(multiple-of 2) n
		"#n pairs"
	:(multiple-of 3) n
		"#n triples"
	else
		'other

. foo 8
. foo 9
. foo 11

destructuring

Mason supports simple object destructuring.

It currently does not support renaming or default values, but that's planned.

{a b} = {a. 1 b. 2}
+ a b

Array destructuring is planned.

In the future, Mason will unify destructuring with patterns and support both within cases, assignments, and parameters.

Nested patterns {p1. :Point x1 y1 p2. :Point x2 y2} are also planned.