gpt4 book ai didi

f# - 通过函数调用进行模式匹配

转载 作者:行者123 更新时间:2023-12-02 21:35:56 32 4
gpt4 key购买 nike

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

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

有效:元组与 (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))

作为函数调用一部分的模式匹配是否仅限于元组?有没有办法对付受歧视的工会?

最佳答案

您需要额外的括号:

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

关于f# - 通过函数调用进行模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21387954/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com