Destructuring
Destructuring is a way to extract nested data from values.
In the pattern matching section you destructured a custom type with case/of:
Since this custom type only has one variant, you can also destructure it with let/in:
Or even directly in your function arguments:
You can use destructring to get data from other types of values as well.
Destructuring Records
When destructuring records, you only have to specify the fields you care about:
If you want to use the same name as a record field, you can use a syntax similar to javascript:
You can give your destructured data an alias with the as
keyword:
All of these techniques also work with function arguments and case/of expressions.
Destructuring Arrays
You can destructure arrays as well:
There is no way to match things like “all the remaining elements” of an array, but you can use Array functions to achieve similar results.