F# Incomplete pattern matches on this expression when using "when"..Why?
Asked Answered
V

1

15

I have this simple F# function:

let compareNum x =
    let y = 10
    match x with
    | _ when x = y -> 0
    | _ when x > y -> 1
    | _ when x < y -> -1

However, F# compiler gives me "Incomplete pattern matches on this expression" warning. In this case, all cases should cover every pattern.

I also see a similar example in "Pattern Matching" section in the 1st edition of Programming F# book by Chris Smith. So something might be changed in the later version of F#?

Voluntarism answered 9/9, 2013 at 4:47 Comment(4)
The compiler assumes that any code with when guards is an incomplete match. It is inelegant, and produces false positives like you saw.Orwin
possible duplicate of Incomplete pattern matching a tuple in F#Orwin
Thanks! I like how one of the answer in the above question said, "In general, it is an anti-pattern to have a when guard in the last pattern." I think that makes sense now.Voluntarism
This is one of those cases where using "if" is not only more idiomatic--it's also a far better way to code the test.Erudite
J
19

I think the answer to the previous question (and the comments -- "In general, it is an anti-pattern to have a when guard in the last pattern" -- by kimsk) explain the situation.

However, I would not say that having a guard in the last pattern is an anti-pattern - it is the easiest workaround, but I find this somewhat unfortunate, because the when pattern gives you useful information about the values you can expect - and that makes understanding the program easier. Last time I had this problem, I left it there, at least as a comment:

let compareNum x =
  let y = 10
  match x with
  | _ when x = y -> 0
  | _ when x > y -> 1
  | _ (*when x < y*) -> -1
Judi answered 9/9, 2013 at 5:52 Comment(1)
+1, if you had the equality check -> 0 as the final pattern it might go some way towards preserving readability without the comment - but personally I think I like this better.Carven

© 2022 - 2024 — McMap. All rights reserved.