Question

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?

Was it helpful?

Solution

You need extra parens:

let f (A.X(a, b)) = a + b
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top