19

After reading Chris' answer to F# - public literal and the blog post at http://blogs.msdn.com/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

This keeps printing "3", although I think it shouldn't. What is it that I don't see here?

4 Answers 4

24

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

And then just write

let one = 1
let two = 2

let trymatch x =
    match x with
    | Equals one -> printfn "%A" one
    | Equals two -> printfn "%A" two
    | _ -> printfn "none"
1
  • 2
    Yes, confirmed. Compiler warning me about all sorts of obvious stuff, but when you really need it... Oct 8, 2010 at 11:43
14

The other answers are right - you must start your identifier with an uppercase letter. See section 7.1.2 of the spec (Named Patterns), which states that:

If long-ident is a single identifier that does not begin with an uppercase character then it is always interpreted as a variable-binding pattern and represents a variable that is bound by the pattern

2
  • I admire your encyclopedic knowledge of the spec, trying to get there but still a few months off, as it seems... just wondering: in this case, wouldn't it have been more straightforward to base the whole mechanism on the case of the first letter, i.e. uppercase=matching, lowercase=binding; sharing the responsibility between the attribute and the identifier case seems to make it more complicated than necessary. Oct 8, 2010 at 11:53
  • 2
    @Alexander - I agree that this behavior is confusing - I don't know why the team chose the approach that they did. It also seems like given the current behavior, some additional compiler warnings on lowercase literals or matches using variable names that shadow literals would be warranted.
    – kvb
    Oct 8, 2010 at 12:49
8

Also if you don't want to have Uppercase literals you can put them in a module (here named Const):

module Const =
    [<Literal>]
    let one = 1
    [<Literal>]
    let two = 2

let trymatch x =
    match x with
    | Const.one -> printfn "%A" Const.one
    | Const.two -> printfn "%A" Const.two
    | _ -> printfn "none"

trymatch 3
4
  • 2
    Any idea if there's a rationale behind that behaviour? Oct 8, 2010 at 11:45
  • @Alexander: I don't know. Notice that module name here doesn't need to be uppercased..
    – elmattic
    Oct 8, 2010 at 11:49
  • 1
    @Alexander - that behavior is in the spec too. Presumably the reasoning is that if it's a compound identifier containing a . then it's certainly not a variable, so you proceed with name resolution.
    – kvb
    Oct 8, 2010 at 11:53
  • @kvb This presumption is certainly consistent with the fact that this solution stops working as soon as you make the match itself part of the module (i.e. indent the function). You need to get rid of the "Const." then to make it syntactically correct, but that re-introduces the ambiguity, which appears to always be resolved in favour of binding over matching. Just tried that case. Anyways, interesting insights today, thanks all. Oct 8, 2010 at 12:07
2

Don't ask me why, but it works when you write your literals uppercase:

[<Literal>]
let One = 1
[<Literal>]
let Two = 2

let trymatch (x:int) =
    match x with
    | One -> printfn "%A" One
    | Two -> printfn "%A" Two
    | _ -> printfn "none"

trymatch 3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.