Matching patterns on function call

F # assigns function arguments using pattern matching. That's why

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

works: The tuple maps to (a, b), and a and b are available directly inside f.

Doing the same with discriminated associations will be equally useful, but I cannot do this:

// 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 a function call limited to tuples? Is there a way to do this with discriminatory unions?

+2
source share
1 answer

You need additional partners:

let f (A.X(a, b)) = a + b
+5
source

Source: https://habr.com/ru/post/1524064/


All Articles