在阅读完克里斯对 F#-公共文字的回答以及 blogs.msdn /b/chrsmith/archive/2008/10/03/f-zen-the-literal-attribute.aspx 我不明白为什么以下各项不起作用:
After reading Chris' answer to F# - public literal and the blog post at blogs.msdn/b/chrsmith/archive/2008/10/03/f-zen-the-literal-attribute.aspx I don't get why the following is not working:
[<Literal>] let one = 1 [<Literal>] let two = 2 let trymatch x = match x with | one -> printfn "%A" one | two -> printfn "%A" two | _ -> printfn "none" trymatch 3尽管我认为不应该,但它始终打印"3".我在这里看不到什么?
This keeps printing "3", although I think it shouldn't. What is it that I don't see here?
推荐答案我认为文字必须为大写.以下工作正常:
I think that literals need to be Uppercase. The following works fine:
[<Literal>] let One = 1 [<Literal>] let Two = 2 let trymatch x = match x with | One -> printfn "%A" One | Two -> printfn "%A" Two | _ -> printfn "none" trymatch 3此外,如果您想要一个很好的通用解决方案而不使用文字,则可以定义一个参数化的活动模式,如下所示:
In addition, if you want a nice general solution for this without using literals, you can define a parameterized active pattern like this:
let (|Equals|_|) expected actual = if actual = expected then Some() else None然后写
let one = 1 let two = 2 let trymatch x = match x with | Equals one -> printfn "%A" one | Equals two -> printfn "%A" two | _ -> printfn "none"更多推荐
文字属性不起作用
发布评论