There are two types of Active Patterns that somewhat differ in usage - Complete and Partial.
Complete Active Patterns can be used when you are able to enumerate all the outcomes, like "is a number odd or even?"
let (|Odd|Even|) number = if number % 2 = 0 then Even else Odd
Notice the Active Pattern definition lists both possible cases and nothing else, and the body returns one of the listed cases. When you use it in a match expression, this is a complete match:
let n = 13 match n with | Odd -> printf "%i is odd" n | Even -> printf "%i is even" n
This is handy when you want to break down the input space into known categories that cover it completely.
Partial Active Patterns on the other hand let you explicitly ignore some possible results by returning an
option. Their definition uses a special case of
_ for the unmatched case.
let (|Integer|_|) str = match Int32.TryParse(str) with | (true, i) -> Some i | _ -> None
This way we can match even when some cases cannot be handled by our parsing function.
let s = "13" match s with | Integer i -> "%i was successfully parsed!" i | _ -> "%s is not an int" s
Partial Active Patterns can be used as a form of testing, whether the input falls into a specific category in the input space while ignoring other options.