Pattern Matching
When dealing with custom types, you likely want to do different things based on the actual value of the custom type. The case of
expression provides this flexibility.
In gren, pattern matching is exhaustive. That means the compiler will complain if you don’t cover all possible cases. This is very helpful, since you can add a variant to your custom type without worrying that you forgot to handle it somewhere.
If you don’t need to handle all possible cases explicitly, you can use _
as a catch-all:
You can use pattern matching on other things than just custom types. Like integers:
Or even records:
Patterns with Data
You can use pattern matching to extract nested data from your values.
If you have a custom type with data, you can give that data a name and use it in the branch for that pattern:
Extracting nested data like this is called desctructuring.