We've been applying pattern matching toliteralvalues at this point. But pattern matching is frequently done on more complicated types, where we can use the cases of the pattern to decompose the data type into its constituent parts. Here we'll talk about Unison data types in the context of pattern matching.
Let's take a look at a pre-made data type, In Unison, Either is defined in the base library like this (we'll break down what these parts mean shortly): The keyword Type parameters in Unison are lowercase by convention. For example: On the right hand side of the equals are thedata constructorsof the type. We use data constructors to create a value of the type being described, so to create an We'll return toexplore more in-depth about data typeslater.Your first Unison data type
Either
.Either is used to represent situations in which a value can be one type or another.structural type Either a b
structural type Either a b
= lib.base.Either.Right b
| lib.base.Either.Left a
type
indicates that we're looking at a type definition, as opposed to aterm definition.Unison types are given a modifier ofstructural
orunique
--structuralhere means that types which share Either's structure are treated as identical to Either and therefore are interchangeable in Unison code, even if they're given a different name.Either
is the name we've given to our type, and the two lettersa
andb
are type parameters.type Box a
type Box a = languageReference.destructuringBinds.Box.Box a
a
andb
as placeholders which represent any type. When we construct a value of type Either, we "fill in" the placeholder.Either
we have two options:Left
orRight
.They're separated by pipes|
.When you see the pipe think "or."
With our whirlwind intro to the parts of a data type behind us, we'll return to how to pattern match on the different data constructors of a given type.
Let's say we wanted a function to tell us which utensils should be paired with a lunch order. We'll use the following types:
type Lunch
type Lunch
= fundamentals.controlFlow.patternMatching.Lunch.Soup Text
| fundamentals.controlFlow.patternMatching.Lunch.Salad Text
| fundamentals.controlFlow.patternMatching.Lunch.Mystery
Text Boolean
type Utensil
type Utensil
= fundamentals.controlFlow.patternMatching.Utensil.Fork
| fundamentals.controlFlow.patternMatching.Utensil.Knife
| fundamentals.controlFlow.patternMatching.Utensil.Spoon
Our function should take in a typeLunch
as an argument and return aList
of typeUtensil
.We know that there are only three ways to make a value of typeLunch
,so we match on the data constructor name followed by the number of fields that the constructor contains.
placeSetting : Lunch -> [Utensil]
placeSetting = cases
Soup soupName -> [Spoon]
Salad saladName -> [Fork, Knife]
_ -> [Spoon, Fork, Knife]
Pattern matching on the data constructors of the type enables us to inspect and make use of the values they contain. In the example above we don't end up using the variables that are bound to the fields in the data, so we could have also represented them as underscores, likeSoup _ -> [Spoon]
,but we can imagine a function where that would become important:
placeSetting : Lunch -> [Utensil]
placeSetting = cases
Soup "Hearty Chunky Soup" -> [Fork, Spoon]
Soup _ -> [Spoon]
Salad _ -> [Fork, Knife]
Mystery mysteryMeal isAlive ->
use Text ==
if (mysteryMeal == "Giant Squid") && isAlive then [Knife]
else [Spoon, Fork, Knife]
_ -> [Spoon, Fork, Knife]
The first case is an example of how to combine a literal pattern match with a data constructor, and the second and third cases are an example of how to match on any value thatSoup
orSalad
data constructor might enclose. Our last case extracts the values being provided to theMystery
data constructor aspattern match variablesfor use on the right.
Note, the underscores above represent the fact that the value being provided to the data constructor isn't important for the logic of our expression on the right. The underscores do, however, need to be present. Every parameter to the data constructor needs to be represented in the pattern either by a variable, as in ourMystery
case, or by an underscore, otherwise Unison will return a patternaritymismatch error.