有没有办法通过 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