, - , switch. , . Active Patterns , Black, White Other . "" , . ,
open System.Drawing
let (|Black|White|Other|) (color:Color) =
if color = Color.Black then Black
elif color = Color.White then White
else Other
let testColor c =
match c with
| Black -> 1
| White -> 0
| Other -> failwith "unexpected color"
, , , 1 0, :
let (|KnownColor|_|) (color:Color) =
if color = Color.Black then Some(1)
elif color = Color.White then Some(0)
else None
let testColor2 c =
match c with
| KnownColor i -> i
| _ -> failwith "unexpected color"
switch, :
let (|Equals|_|) (lhs) (rhs) =
if lhs = rhs then Some(lhs) else None
let testColor3 c =
match c with
| Equals Color.Black _ -> 1
| Equals Color.White _ -> 0
| _ -> failwith "unexpected color"
let testString c =
match c with
| Equals "Hi" _ -> 1
| Equals "Bye" _ -> 0
| _ -> failwith "unexpected string"