📜Recap
Pattern matching
F♯ core building block
Combines "data structure matching" and "deconstruction"
Used almost everywhere:
match
andfunction
expressionstry/with
blocklet
binding, including function parameter
Can be abstracted into a
fold
function associated with a union type
Patterns
Constant • Identifier • Wilcard
1
, Color.Red
• Some 1
• _
Collection : Cons • List • Array
head :: tail
• [1; 2]
Product type : Record • Tuple
{ A = a }
• a, b
Type Test
:? Subtype
Logical : OR, AND
1 | 2
, P1 & P2
Variables • Alias
head :: _
• (0, 0) as origin
+ The when
guards in match
expressions
Active Patterns
Extending pattern matching
Based on function + metadata → 1st-class citizens
4 types: total simple/multiple, partial (simple), parametric
At 1st little tricky to understand, but we get used to it quickly
Use for:
Add semantics without relying on union types
Simplify / factorize guards
Wrapping BCL methods
Extract a data set from an object
...
Addendum
📜 Match expressions, F# for fun and profit, Juin 2012
📜 Domain modelling et pattern matching : Roman numeral, F# for fun and profit, Juin 2012
📜 Recursive types and folds (6 articles), F# for fun and profit, Août 2015
Last updated
Was this helpful?