let x = true match x with | true -> printfn "x is true"
C:\Program Files (x86)\Microsoft VS Code\Untitled-1(2,7): warning FS0025: Incomplete pattern matches on this expression. For example, the value 'false' may indicate a case not covered by the pattern(s).
This is because not all of the possible bool values were covered.
let x = 5 match x with | 1 -> printfn "x is 1" | 2 -> printfn "x is 2" | _ -> printfn "x is something else"
here we use the special
_ character. The
_ matches all other possible cases.
_can get you into trouble
consider a type we create ourselves it looks like this
type Sobriety = | Sober | Tipsy | Drunk
We might write a match with expession that looks like this
match sobriety with | Sober -> printfn "drive home" | _ -> printfn "call an uber"
The above code makes sense. We are assuming if you aren't sober you should call an uber so we use the
_ to denote that
We later refactor our code to this
type Sobriety = | Sober | Tipsy | Drunk | Unconscious
The F# compiler should give us a warning and prompt us to refactor our match expression to have the person seek medical attention. Instead the match expression silently treats the unconscious person as if they were only tipsy. The point is you should opt to explicitly list out cases when possible to avoid logic errors.