F#模式匹配

Mir*_*anu 3 .net f# pattern-matching

我很困惑F#中的模式匹配是如何工作的let.我正在使用Visual Studio的'F#interactive'窗口,F#版本1.9.7.8.假设我们定义一个简单类型:

type Point = Point of int * int ;;
Run Code Online (Sandbox Code Playgroud)

并尝试模式匹配Point使用的值let.

let Point(x, y) = Point(1, 2) in x ;;
Run Code Online (Sandbox Code Playgroud)

失败了error FS0039: The value or constructor 'x' is not defined.一个人应该如何使用模式匹配let

最奇怪的是:

let Point(x, y) as z = Point(1, 2) in x ;;
Run Code Online (Sandbox Code Playgroud)

按预期返回1.为什么?

Joh*_*bom 10

你需要在你的模式周围加上括号:

let (Point(x, y)) = Point(1, 2) in x ;;
Run Code Online (Sandbox Code Playgroud)

否则就无法将模式与功能绑定区分开来......