Parsing/Typing of as-patterns in F#

93 views Asked by At

I'm trying to use an as-pattern to decompose a tuple value, and am observing some weird parsing/typing behaviour. Example fsi (4.0) run:

> let t = (0, (1, 2), 3) in let (_, (_, _) as pair, _) = t in ();;

  let t = (0, (1, 2), 3) in let (_, (_, _) as pair, _) = t in ();;
  -------------------------------------------------------^

stdin(9,56): error FS0001: Type mismatch. Expecting a
    ('a * ('b * 'c)) * 'd
but given a
    int * (int * int) * int
The type ''a * ('b * 'c)' does not match the type 'int'

However, the example works if I put in some additional ():

> let t = (0, (1, 2), 3) in let (_, ((_, _) as pair), _) = t in ();;
val it : unit = ()

Is this me being slightly daft, or is the parser misbehaving in the first example? I guess it tries to apply the "as pair" to ", (, _)", which is slightly more greedy than seems right.

1

There are 1 answers

0
Random Dev On

, has higher precedence than as that's why you need additional (,) in

> let t = (0, (1, 2), 3) in let (_, ((_, _) as pair), _) = t in pair;;
val it : int * int = (1, 2)

you can see this from the error:

stdin(9,56): error FS0001: Type mismatch. Expecting a ('a * ('b * 'c)) * 'd but given a int * (int * int) * int

it tells you basically that misplaced some parentheses ;)