F#通过模式匹配分配函数参数。这就是为什么
// ok: pattern matching of tuples upon function call
let g (a,b) = a + b
g (7,4)
作品:元组与(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))
模式匹配作为函数调用的一部分是否仅限于元组?有没有办法让受歧视的工会做这件事?
发布于 2014-01-27 09:52:18
你需要额外的父母:
let f (A.X(a, b)) = a + b
https://stackoverflow.com/questions/21387954
复制