Matching literals
One of the simplest cases of matching patterns is a pattern represented by a literal and assuming a simple comparison-expression
value equality. Literals can be of any numeric, character, or string types. They can also be cases of a .NET enumeration (each such case is inherently a symbolic name alias of the integer value) or a value decorated with the [<Literal>]
attribute.
In the following script, I can easily match int
literals and the int
value aliased as THREE
, decorated with the [<Literal>]
attribute (Ch4_1.fsx
):
[<Literal>] let THREE = 3 let transformA v = match v with | 1 ->"1" | 2 ->"2" | THREE ->"3" transformA <| (1 + 2)
This yields string "3"
, as expected. However, it wouldn't be possible to mix int
literals with named int
constant values from the following script (Ch4_1.fsx
):
type Multiples = | Zero = 0 | Five = 5 let transformB ``compare me`` = match ``compare...