【发布时间】:2012-12-12 14:31:26
【问题描述】:
是否可以在有区别的联合类型声明中使用活动模式?
更准确地说,考虑以下玩具示例:
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!"
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
This answer 可能是相关的:“不应将活动模式用作成员。这些编译器根本是编译器错误”
标签: f# discriminated-union active-pattern