f# - Literal Attribute not working -
after reading chris' answer f# - public literal , blog post @ http://blogs.msdn.com/b/chrsmith/archive/2008/10/03/f-zen-the-literal-attribute.aspx don't why following not working:
[<literal>] let 1 = 1 [<literal>] let 2 = 2 let trymatch x = match x | 1 -> printfn "%a" 1 | 2 -> printfn "%a" 2 | _ -> printfn "none" trymatch 3
this keeps printing "3", although think shouldn't. don't see here?
i think literals need uppercase. following works fine:
[<literal>] let 1 = 1 [<literal>] let 2 = 2 let trymatch x = match x | 1 -> printfn "%a" 1 | 2 -> printfn "%a" 2 | _ -> printfn "none" trymatch 3
in addition, if want nice general solution without using literals, can define parameterized active pattern this:
let (|equals|_|) expected actual = if actual = expected some() else none
and write
let 1 = 1 let 2 = 2 let trymatch x = match x | equals 1 -> printfn "%a" 1 | equals 2 -> printfn "%a" 2 | _ -> printfn "none"
Comments
Post a Comment