I have a discriminated union like this:
Type Result =
| Good of bool | Bad of bool
In many cases I know that result is Good. To unwrap the Result, I have to use pattern matching for just the Good option. As a result I get a warning (not an error) that says "Incomplete pattern match on this expression..". Is there a way to unwrap is without having to use pattern matching?
A discriminated union is a union data structure that holds various objects, with one of the objects identified directly by a discriminant. The discriminant is the first item to be serialized or deserialized. A discriminated union includes both a discriminant and a component.
Discriminated unions are useful for heterogeneous data; data that can have special cases, including valid and error cases; data that varies in type from one instance to another; and as an alternative for small object hierarchies.
You can just use let
, e.g.
type Result =
| Good of bool
| Bad of bool
let example = Good true
let (Good unwrappedBool) = example
Note that this will still result in a compiler warning that the match cases might be incomplete.
Technically, however, this is still using pattern matching, just doing so without a match
expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With