David Teller wrote:
> Dear list,
>
> I'm currently working on a little Camlp4 extension which has to often
> generate pattern-matching clauses depending on user code -- and deal
> with match failures accordingly.
>
> Now, I guess
>
> 1. I can wrap the user's pattern-matching inside a try...with, catch any
> Match_failure and deal with it.
This seems hackish.
> 2. I can add a catch-all clause " _ -> deal_with_error ". While the
> semantics of this rewriting are exactly what I need, the compiler tends
> to print "Warning U: this match case is unused" whenever the user has
> already taken care of all cases.
It may seem that you've taken care of all cases, but keep in mind that
the compiler looks at a match case containing a 'when' clause and
assumes that clause can cause the match to fail, independent of all
other terms.
i.e. if you write:
match 123 with
| x when x mod 2 = 1 -> "Odd"
| x when x mod 2 = 0 -> "Even"
The compiler can't see that you've covered all cases. Even the
following generates a warning:
match 123 with x when true -> "TRUE" | x when false -> "FALSE"
The way I write this style of matching goes like this:
match 123 with
| x when x mod 2 = 1 -> "Odd"
| x (* x mod 2 = 0 *) -> "Even"
Can you structure your matches this way?
E.