F# 通过函数调用进行模式匹配
F#通过模式匹配分配函数参数。这就是为什么F# 通过函数调用进行模式匹配,f#,pattern-matching,f#-3.0,F#,Pattern Matching,F# 3.0,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 l
// 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))
模式匹配作为函数调用的一部分是否仅限于元组?有没有办法解决歧视工会的问题?您需要额外的条件:
let f (A.X(a, b)) = a + b