Patterns
Used extensively in Fβ―
match expression, let binding, parameter deconstruction...
Very practical for manipulating Fβ― algebraic types (tuple, record, union)
Composable: supports multiple nesting levels
Combined using logical AND/OR
Supports literals:
1.0,"test"...
Wildcard Pattern
Represented by _, alone or combined with another pattern.
Always true β To be placed last in a match expression.
β οΈ Always seek 1st to handle all cases exhaustively/explicitly
When impossible, then use _
match option with
| Some 1 -> ...
| _ -> ... // β οΈ Non exhaustive
match option with
| Some 1 -> ...
| Some _ | None -> ... // π More exhaustiveRecommendation
Always seek first to handle all cases exhaustively/explicitly
When it's impossible (by combination explosion), too ugly to read or too boring to write
Then use
_to discard all other cases
Constant Pattern
Detects constants, null and number literals, char, string, enum.
β Notes :
The
Threepattern is also classified as an Identifier Pattern πFor
nullmatching, we also talk about Null Pattern.
Variable Pattern
Assigns the detected value to a "variable" for subsequent use
Example: b variable below
β οΈ You cannot link to the same variable more than once.
π‘ Solution: use 2 variables then check their equality
Identifier Pattern
Detects cases of a union type and their possible contents
The identifier pattern can be combined with both constant and variable patterns. When the cases fields have labels, we can pattern match on them too:
OR / AND Patterns
Combine two patterns (named P1 and P2 below):
P1 | P2β P1 or P2 - E.g.Rectangle (0, _) | Rectangle (_, 0)P1 & P2β P1 and P2 - used especially with active patterns π
βοΈ Explanations:
The
Titleis optional. The first pattern{ Title = Some name }will match only if theTitleis specified.The second pattern
{ Filename = name }will always works. It's written to extract theFilenameinto the same variablenameas the first pattern.
Alias Pattern
as is used to name an element whose content is deconstructed
π‘ Also works within functions to get back the parameter name, person below:
π‘ The AND pattern can be used as an alternative to the alias pattern. βοΈ However, it won't work to get the parameter name:
Parenthesized Pattern
Purpose: Use of parentheses () to group patterns
Common use case: tackle precedence
β Line β would compile without doing () :: tail
β οΈ Parentheses complicate reading
π‘ Try to do without when possible:
Construction Patterns
Use type construction syntax to deconstruct a type
Cons and List Patterns
Array Pattern
Tuple Pattern
Record Pattern
Cons and List Patterns
β Inverses of the 2 ways to construct a list
Cons Pattern : head :: tail β decomposes a list (with >= 1 element) into:
Head: 1st element
Tail: another list with the remaining elements - can be empty
List Pattern : [items] β decomposes a list into 0..N items
[]: empty list[x]: list with 1 element set in thexvariable[x; y]: list with 2 elements set in variablesxandy[_; _]: list with 2 elements ignored
π‘ x :: [] β‘ [x], x :: y :: [] β‘ [x; y]...
The default match expression combines the 2 patterns:
β A list is either empty [], or composed of an item and the rest: head :: tail
Example:
β Recursive functions traversing a list
β The [] pattern is used to stop recursion:
Array Pattern
Syntax: [| items |] for 0..N items between ;
β There is no pattern for sequences, as they are "lazy ".
Tuple Pattern
Syntax: items or (items) for 2..N items between ,.
π‘ Useful to match several values at the same time
Record Pattern
Syntax: { Field1 = var1; ... }
It's not required to specify all fields in the record
In case of ambiguity on the record type, qualify the field:
Record.Field
π‘ Also works for function parameters:
β οΈ Reminder: there is no pattern for anonymous Records!
Type Test Pattern
Syntax: my-object :? sub-type and returns a bool
β β my-object is sub-type in Cβ―
Usage: with a type hierarchy
βοΈ Note: the :? operator is not designed to check evidence, which can be confusing.
β Example: (from stackoverflow)
try/with block
try/with blockThis pattern is common in try/with blocks:
Boxing
The Type Test Pattern only works with reference types. β For a value type or unknown type, it must be boxed.
Last updated
Was this helpful?