views:

112

answers:

1

I'm puzzled by how pattern matching works in F# for let. I'm using the Visual Studio 'F# interactive' window, F# version 1.9.7.8. Say we define a simple type:

type Point = Point of int * int ;;

and the try to pattern match against values of Point using let.

let Point(x, y) = Point(1, 2) in x ;;

fails with error FS0039: The value or constructor 'x' is not defined. How is one supposed to use pattern matching with let?

The most curious thing is that:

let Point(x, y) as z = Point(1, 2) in x ;;

returns 1 as expected. Why?

+9  A: 

You need to put parenthesis around your pattern:

let (Point(x, y)) = Point(1, 2) in x ;;

Otherwise there's no way to distinguish the pattern from a function-binding...

Johan Kullbom