【问题标题】:Pattern matching by function call通过函数调用进行模式匹配
【发布时间】:2014-02-18 17:02:53
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: f# pattern-matching f#-3.0


    【解决方案1】:

    你需要额外的括号:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多