Does F# support pattern matching of a discriminated union member instance by criteria other than the Identifier pattern?
For example, imagine that I want to match on the underlying shape of the data and I want to consider anything with an int * int
shape, regardless of how the DU classifies the value. Is
Here's how I'd 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'd like something closer to the following:
let shapeName =
match asCase1 with
| (x,y) -> "pair"
| _ -> "atom"
Is there some similarly expressive syntax that is currently supported in F# or am I stuck with explicitly specifying all cases?
Note: I know that I could figure out how to find the information that I want with reflection, but I'm not interested in such a solution.
type Expr = | T | F | NOT of Expr | AND of Expr * Expr | OR of Expr * Expr
. It manifests in 3 shapes from 5 symbols (2 constants, 1 function, 2 binary operations). Any evaluation procedure would use a distinct pattern for interpreting each node, based on if it were: a constant; a function; or, a binary operation. Depending on what you're doing with the expression you may be interested in whether an expression isAND
/OR
or you might only care that it's binary. In the 2nd case, matching on shape could easily save 100s of LOC – Daredeviltry