I want to write a pattern matching like the follows:
match ... with
...
| Const1 (r, c) | Const2 (m, n)
-> expr
It returns an error: Error: Variable c must occur on both sides of this | pattern.
Do I have to write expr twice (one time for Const1, the other time for Const2)? Could anyone help?
As the error message said, Or pattern (
| pattern) requires binding to the same set of variables. Therefore:or
would work.
Of course, you only can do that if
Const1andConst2accept the same type. In some cases, you still do so if you have parts of constructs with the same type:The pitfall of Or pattern is that you don’t know which constructor you are in. So if logic of
exprdepends onConst1orConst2, you couldn’t use Or pattern anymore.