[英]F# pattern matching
我是通过模式匹配在F#是如何工作的困惑let
。 我正在使用Visual Studio的'F#interactive'窗口,F#版本1.9.7.8。 假设我们定义一个简单类型:
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。 为什么?
你需要在你的模式周围加上括号:
let (Point(x, y)) = Point(1, 2) in x ;;
否则就无法将模式与功能绑定区分开来......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.