|
@@ -165,6 +165,8 @@ in case you want to take a look under the hood.
|
|
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| match | See match_. |
|
|
| match | See match_. |
|
|
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
|
|
+| when | Used by `pattern guards <Pattern guards_>`_ in ``match`` statements. |
|
|
|
|
++------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| break | Exits the execution of the current ``for`` or ``while`` loop. |
|
|
| break | Exits the execution of the current ``for`` or ``while`` loop. |
|
|
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| continue | Immediately skips to the next iteration of the ``for`` or ``while`` loop. |
|
|
| continue | Immediately skips to the next iteration of the ``for`` or ``while`` loop. |
|
|
@@ -1654,10 +1656,10 @@ Basic syntax
|
|
|
|
|
|
::
|
|
::
|
|
|
|
|
|
- match <expression>:
|
|
|
|
|
|
+ match <test value>:
|
|
<pattern(s)>:
|
|
<pattern(s)>:
|
|
<block>
|
|
<block>
|
|
- <pattern(s)> when <guard expression>:
|
|
|
|
|
|
+ <pattern(s)> when <pattern guard>:
|
|
<block>
|
|
<block>
|
|
<...>
|
|
<...>
|
|
|
|
|
|
@@ -1790,9 +1792,13 @@ The following pattern types are available:
|
|
Pattern guards
|
|
Pattern guards
|
|
""""""""""""""
|
|
""""""""""""""
|
|
|
|
|
|
|
|
+A *pattern guard* is an optional condition that follows the pattern list
|
|
|
|
+and allows you to make additional checks before choosing a ``match`` branch.
|
|
|
|
+Unlike a pattern, a pattern guard can be an arbitrary expression.
|
|
|
|
+
|
|
Only one branch can be executed per ``match``. Once a branch is chosen, the rest are not checked.
|
|
Only one branch can be executed per ``match``. Once a branch is chosen, the rest are not checked.
|
|
If you want to use the same pattern for multiple branches or to prevent choosing a branch with too general pattern,
|
|
If you want to use the same pattern for multiple branches or to prevent choosing a branch with too general pattern,
|
|
-you can specify a guard expression after the list of patterns with the ``when`` keyword::
|
|
|
|
|
|
+you can specify a pattern guard after the list of patterns with the ``when`` keyword::
|
|
|
|
|
|
match point:
|
|
match point:
|
|
[0, 0]:
|
|
[0, 0]:
|
|
@@ -1808,9 +1814,9 @@ you can specify a guard expression after the list of patterns with the ``when``
|
|
[var x, var y]:
|
|
[var x, var y]:
|
|
print("Point (%s, %s)" % [x, y])
|
|
print("Point (%s, %s)" % [x, y])
|
|
|
|
|
|
-- If there is no matching pattern for the current branch, the guard expression
|
|
|
|
|
|
+- If there is no matching pattern for the current branch, the pattern guard
|
|
is **not** evaluated and the patterns of the next branch are checked.
|
|
is **not** evaluated and the patterns of the next branch are checked.
|
|
-- If a matching pattern is found, the guard expression is evaluated.
|
|
|
|
|
|
+- If a matching pattern is found, the pattern guard is evaluated.
|
|
|
|
|
|
- If it's true, then the body of the branch is executed and ``match`` ends.
|
|
- If it's true, then the body of the branch is executed and ``match`` ends.
|
|
- If it's false, then the patterns of the next branch are checked.
|
|
- If it's false, then the patterns of the next branch are checked.
|