Active pattern matching discriminatory unions

Can I use the following discriminatory union with active pattern matching? I could not find any examples.

This is what I am trying to do:

type c = a | b

type foo =
  | bar1
  | bar2 of c

//allowed
let (|MatchFoo1|_|) aString =
  match aString with
  | "abcd" -> Some bar1
  | _ -> None

//not allowed
let (|MatchFoo2|_|) aString =
  match aString with
  | "abcd" -> Some (bar2 of a)
  | _ -> None

Why can't Some be used in second order? Is there any other way to achieve the same?

+4
source share
1 answer

You need to use it ofwhen declaring a type, so you can just build the values ​​using the constructor bar2, for example:

bar2 a

Your second function should work if you change it to:

let (|MatchFoo2|_|) aString =
  match aString with
  | "abcd" -> Some (bar2 a)
  | _ -> None
+6
source

All Articles