【发布时间】:2014-02-18 17:02:53
【问题描述】:
F# 通过模式匹配分配函数参数。这就是为什么
// ok: pattern matching of tuples upon function call
let g (a,b) = a + b
g (7,4)
works: 元组与 (a,b) 匹配,a 和 b 在 f 中直接可用。
对受歧视的工会做同样的事情同样有益,但我无法做到:
// error: same with discriminated unions
type A =
| X of int * int
| Y of string
let f A.X(a, b) = a + b // Error: Successive patterns
// should be separated by spaces or tupled
// EDIT, integrating the answer:
let f (A.X(a, b)) = a + b // correct
f (A.X(7, 4))
作为函数调用的一部分的模式匹配是否仅限于元组?有没有办法用受歧视的工会做到这一点?
【问题讨论】:
标签: f# pattern-matching f#-3.0