Reputation: 11060
F# assigns function arguments via pattern matching. This is why
// ok: pattern matching of tuples upon function call
let g (a,b) = a + b
g (7,4)
works: The tuple is matched with (a,b) and a and b are available directly inside f.
Doing the same with discriminated unions would be equally beneficial, but I cannot get it to done:
// 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))
Is pattern matching as part of the function call limited to tuples? Is there a way to do it with discriminated unions?
Upvotes: 2
Views: 290