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 ...

Get F# 4.0 Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.