通过函数调用匹配模式

cit*_*kid 2 f# pattern-matching f#-3.0

F#通过模式匹配来分配函数参数.这就是为什么

// ok: pattern matching of tuples upon function call
let g (a,b) = a + b
g (7,4)
Run Code Online (Sandbox Code Playgroud)

有效:元组与(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))
Run Code Online (Sandbox Code Playgroud)

模式匹配是函数调用的一部分,仅限于元组吗?有没有办法与受歧视的工会一起做?

Dan*_*iel 5

你需要额外的parens:

let f (A.X(a, b)) = a + b
Run Code Online (Sandbox Code Playgroud)