Pattern Guards
Pattern guards allow you to add additional conditional checks after pattern matching. The syntax is case pattern where condition =>. The branch only executes when both the pattern matches and the condition is true.
Pattern guards let you handle different cases under the same pattern. For example, after matching a number, you can handle it differently based on whether it's positive, negative, or zero. This is clearer than writing multiple if-else statements with complex conditions.
Note
Cangjie uses the
where keyword rather than if to define guard conditions, which is a common style in pattern matching.Try changing number on line 2 to different values (like -7, 0, 12) and observe how different guard conditions affect the matching result.