I get a "Error: Warning 25: bad style, all clauses in this pattern-matching are guarded"
What does “guarded” mean?
My code has pattern matching-
match z with
| y when List.length z = 0 -> ...
| y when List.length z > 0 -> ...
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The guards are the
whenparts. What the compiler is telling you is that it can’t tell whether your match is exhaustive (covers all possible cases), but that it might not be. The compiler can’t really tell for sure, because exhaustiveness is undecidable for arbitrary expressions. The compiler just figures you should have at least one pattern without a guard because when the match is exhaustive, a guard on the last case would be redundant.Since you know your match is exhaustive, the compiler is basically right. Your second guard is redundant. You can just leave it off with no difference in meaning:
This will make the compiler happy.
I like this warning; it has found a few logic errors for me over the years.
If this code isn’t just an example but is really what you wrote, it would be much more idiomatic to write it like this:
It’s also a tiny bit more efficient since it won’t bother to calculate the length of the list and then discard the result.
If you don’t need to destructure the list, you can make it simpler still: