有没有办法将调用嵌套到活动模式?
这样的事情:
type Fnord =
| Foo of int
let (|IsThree|IsNotThree|) x =
match x with
| x when x = 3 -> IsThree
| _ -> IsNotThree
let q n =
match n with
| Foo x ->
match x with
| IsThree -> true
| IsNotThree -> false
// Is there a more ideomatic way to write the previous
// 5 lines? Something like:
// match n with
// | IsThree(Foo x) -> true
// | IsNotThree(Foo x) -> false
let r = q (Foo 3) // want this to be false
let s = q (Foo 4) // want this to be true
或是匹配后是另一场比赛的首选方式?
答案 0 :(得分:12)
有效。你只是向后模式。
type Fnord =
| Foo of int
let (|IsThree|IsNotThree|) x =
match x with
| x when x = 3 -> IsThree
| _ -> IsNotThree
let q n =
match n with
| Foo (IsThree x) -> true
| Foo (IsNotThree x) -> false
let r = q (Foo 3) // want this to be true
let s = q (Foo 4) // want this to be false