Compliance pattern F #

I am puzzled at how template matching works in F # for let. I am using the Visual Studio "F # interactive" window, F # version 1.9.7.8. Let's say we define a simple type:

type Point = Point of int * int ;;

and trying to match the pattern with values Pointwith let.

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

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

The most interesting thing is that:

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

returns 1 as expected. Why?

+3
source share
1 answer

You need to put parentheses around your template:

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

Otherwise, there is no way to distinguish a template from a function binding ...

+10
source

Source: https://habr.com/ru/post/1733504/


All Articles