有没有一种方法可以通过它们的case-identifier在F#中比较判别联合?
type MyUnion =
| MyString of string
| MyInt of int
let x = MyString("hello")
let y = MyString("bye")
let z = MyInt(25)
let compareCases a b =
// compareCases x y = true
// compareCases x z = false
// compareCases y z = false
如何以通用的方式实现compareCases
函数?
例如下面的示例,但更加通用(反射也可以):
let compareCases a b =
match a with
| MyString(_) -> match b with | MyString(_) -> true | _ -> false
| MyInt(_) -> match b with | MyInt(_) -> true | _ -> false