I often do pattern matching in a let expression, where I know the result form. It is clear that I cannot expect the compiler to derive this knowledge at all, but perhaps there is a more idiomatic way to do this in a condensed form.
As an example, please check out the following code:
type foo = A of int | B of string
let x = (true, A 0)
let (b, A i) = x in i + 2
Which correctly warns me that the result (_, B _)does not match. A possible way is to explicitly handle the missing case, as in:
let (b,i) = match x with
| (a, A j) -> (a,j+2)
| _ -> failwith "implementation error!"
But this obscures the actual calculation. Is there a shorter option?
Edit: Jeffrey Scofield noted that in the case without nesting, the explicit conversion function works well. Is there also a version for nested type matching?