Active pattern

Category:
Active patterns
Description: Active pattern can save mulitple matches. This sample shows how to avoid use nested match statement.
Code:
let ActivePatternSample() = 
let (|Match|) v i = (v, i)
let tag, num = true, 1
let r = match tag, num with
| true, (Match "ID0" (str, 0) | Match "ID1" (str, 1)) -> sprintf "%s" str
| _ -> "unknown"
printfn "%s" r
Assert.AreEqual(r, "ID0")

Execution Result:
ID1
failwith not equal

Last edited Sep 14, 2011 at 3:17 AM by ttliu2000, version 1

Comments

No comments yet.