Is it possible to match patterns in the basic form of a discriminatory association?

Does the F # pattern matching of an instance of a discriminated party member support criteria other than the identifier pattern ?

For example, imagine that I want to combine a basic data form, and I want to consider something with the form int * int, regardless of how the DU classifies the value. Is an

Here is how I would do it now:

type ExampleDU = 
  | BinaryCase1 of x:int * y:int
  | BinaryCase2 of x:int * y:int
  | UnaryCase1  of x:int

let underlyingValue = (1,2)
let asCase1 = BinaryCase1 underlyingValue
let asCase2 = BinaryCase2 underlyingValue

let shapeName = 
  match asCase1 with
  | BinaryCase1 (x,y) | BinaryCase2 (x,y) -> "pair" // is this possible without explicitly writing the name for each part?
  | _ -> "atom"

I would like something closer to the following:

let shapeName = 
  match asCase1 with
  | (x,y) -> "pair" 
  | _ -> "atom"

Is there some similar expressive syntax that is currently supported in F # or am I stuck in explicitly defining all cases?

Note. I know that I could figure out how to find the information I want with a reflection, but that doesn't interest me.

+1
1

let (|Pair|_|) input = 
    match input with
    |BinaryCase1(a,b)
    |BinaryCase2(a,b) -> Some(a,b)
    | _ -> None

match asCase1 with |Pair(a,b) -> printfn "matched" | _ -> ();;
+4

All Articles