문제

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?

도움이 되었습니까?

해결책

You need extra parens:

let f (A.X(a, b)) = a + b
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top