是否可以在有区别的联合类型声明中使用活动模式?
更准确地说,考虑以下玩具示例:
type T =
| A of int
| B
let (|Negative|_|) t =
match t with
| A n when n < 0 -> Some ()
| _ -> None
let T_ToString = function
| Negative () -> "negative!"
| _ -> "foo!"
现在假设我想覆盖 T 中的 ToString()。在 T 的类型声明中,我不能引用 T_ToString,因为此时尚未声明 T_ToString。我无法在 ToString() 之前移动活动模式和 T_ToString ,因为此时尚未声明 T 。但这也不起作用:
type T =
| A of int
| B
static member (|Negative|_|) t =
match t with
| A n when n < 0 -> Some ()
| _ -> None
override this.ToString () =
match this with
| Negative () -> "negative!"
| _ -> "foo!"