1

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

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"
4

1 回答 1

5
let coerceColor a =
    match box a with
    | :? Color as c -> c
    | :? (int*int*int) as t -> t |> Color.FromArgb
    | _ -> failwith "Cannot coerce color"

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

于 2013-07-02T11:38:58.547 回答