2010-02-19 107 views
3

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

type Point = Point of int * int ;; 

,并使用let的尝试模式匹配反对Point值。

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

因为error FS0039: The value or constructor 'x' is not defined而失败。一个人应该如何使用与let模式匹配?

最奇怪的是:

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

收益1预期。为什么?

回答

10

你需要把括号围绕你的模式:

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

否则没有办法来区分功能结合的模式...