【问题标题】:F# - Pattern Matching on boxed tuplesF# - 盒装元组的模式匹配
【发布时间】:2013-07-02 11:26:00
【问题描述】:

如何在盒装元组上进行模式匹配?或者有没有更好的方法来做这样的事情(简化示例):

open System.Drawing

let coerceColor a =
    match box a with
    | :? Color as c -> c
    | (:? int as r),(:? int as g),(:? int as b) -> Color.FromArgb(r,g,b)
    | _ -> failwith "Cannot coerce color"

【问题讨论】:

    标签: f# pattern-matching


    【解决方案1】:
    let coerceColor a =
        match box a with
        | :? Color as c -> c
        | :? (int*int*int) as t -> t |> Color.FromArgb
        | _ -> failwith "Cannot coerce color"
    

    但如果我可以改变设计,我宁愿使用 DU 或带有重载的静态成员。

    【讨论】:

      猜你喜欢
      • 2020-09-25
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 2023-01-08
      • 1970-01-01
      • 2021-02-21
      • 1970-01-01
      • 2015-01-25
      相关资源
      最近更新 更多