有没有办法嵌套对F#活动模式的调用?

Jam*_*ore 5 f#

有没有办法将调用嵌套到活动模式?

像这样的东西:

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
Run Code Online (Sandbox Code Playgroud)

或者是匹配后跟另一场比赛的首选方式?

gra*_*bot 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
Run Code Online (Sandbox Code Playgroud)