The important thing to note is that A
and B
are constructors of the same type Something
. So you will get inexhaustive pattern matching warning if you try to use A
and B
cases separately.
IMO, deconstructing all cases of DUs is a good idea since it is type-safe and forces you to think of handling those cases even you don't want to. The problem may arise if you have to deconstruct DUs repetitively in the same way. In that case, defining map
and fold
functions on DUs might be a good idea:
let mapSomething fa fb = function
| A(i) -> fa i
| B(s, i) -> fb s i
Please refer to excellent Catamorphism series by @Brian to learn about fold on DUs.
That also said that your example is fine. What you really process are string
and int
values after deconstruction.
You can use Active Patterns to consume two cases separately:
let (|ACase|) = function A i -> i | B _ -> failwith "Unexpected pattern B _"
let (|BCase|) = function B(s, i) -> (s, i) | A _ -> failwith "Unexpected pattern A _"
let doSomethingWithA (ACase i) = ....
but inferred type of doSomethingWithA
is still the same and you get an exception when passing B _
to the function. So it's a wrong thing to do IMO.