Skip to content

Commit 1d11509

Browse files
authored
Merge pull request godotengine#8167 from dalexeev/gds-update-match-patterns-and-literals
GDScript: Update `match` patterns and literal docs
2 parents 8fe3a6a + 6ad7f78 commit 1d11509

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

tutorials/scripting/gdscript/gdscript_basics.rst

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,11 @@ Literals
341341
~~~~~~~~
342342

343343
+---------------------------------+-------------------------------------------+
344-
| **Literal** | **Type** |
344+
| **Example(s)** | **Description** |
345+
+---------------------------------+-------------------------------------------+
346+
| ``null`` | Null value |
347+
+---------------------------------+-------------------------------------------+
348+
| ``false``, ``true`` | Boolean values |
345349
+---------------------------------+-------------------------------------------+
346350
| ``45`` | Base 10 integer |
347351
+---------------------------------+-------------------------------------------+
@@ -363,6 +367,12 @@ Literals
363367
+---------------------------------+-------------------------------------------+
364368
| ``^"Node/Label"`` | :ref:`NodePath <class_NodePath>` |
365369
+---------------------------------+-------------------------------------------+
370+
371+
There are also two constructs that look like literals, but actually are not:
372+
373+
+---------------------------------+-------------------------------------------+
374+
| **Example** | **Description** |
375+
+---------------------------------+-------------------------------------------+
366376
| ``$NodePath`` | Shorthand for ``get_node("NodePath")`` |
367377
+---------------------------------+-------------------------------------------+
368378
| ``%UniqueNode`` | Shorthand for ``get_node("%UniqueNode")`` |
@@ -1437,7 +1447,15 @@ match
14371447
A ``match`` statement is used to branch execution of a program.
14381448
It's the equivalent of the ``switch`` statement found in many other languages, but offers some additional features.
14391449

1440-
Basic syntax::
1450+
.. warning::
1451+
1452+
``match`` is more type strict than the ``==`` operator. For example ``1`` will **not** match ``1.0``. The only exception is ``String`` vs ``StringName`` matching:
1453+
for example, the String ``"hello"`` is considered equal to the StringName ``&"hello"``.
1454+
1455+
Basic syntax
1456+
""""""""""""
1457+
1458+
::
14411459

14421460
match <expression>:
14431461
<pattern(s)>:
@@ -1446,19 +1464,16 @@ Basic syntax::
14461464
<block>
14471465
<...>
14481466

1449-
.. warning::
1450-
1451-
``match`` is more type strict than the ``==`` operator. For example ``1`` will **not** match ``1.0``. The only exception is ``String`` vs ``StringName`` matching:
1452-
for example, the String ``"hello"`` is considered equal to the StringName ``&"hello"``.
1453-
1454-
**Crash-course for people who are familiar with switch statements**:
1467+
Crash-course for people who are familiar with switch statements
1468+
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
14551469

14561470
1. Replace ``switch`` with ``match``.
14571471
2. Remove ``case``.
14581472
3. Remove any ``break``\ s.
14591473
4. Change ``default`` to a single underscore.
14601474

1461-
**Control flow**:
1475+
Control flow
1476+
""""""""""""
14621477

14631478
The patterns are matched from top to bottom.
14641479
If a pattern matches, the first corresponding block will be executed. After that, the execution continues below the ``match`` statement.
@@ -1467,10 +1482,10 @@ If a pattern matches, the first corresponding block will be executed. After that
14671482

14681483
The special ``continue`` behavior in ``match`` supported in 3.x was removed in Godot 4.0.
14691484

1470-
There are 6 pattern types:
1485+
The following pattern types are available:
14711486

1472-
- Constant pattern
1473-
Constant primitives, like numbers and strings::
1487+
- Literal pattern
1488+
Matches a `literal <Literals_>`_::
14741489

14751490
match x:
14761491
1:
@@ -1480,9 +1495,8 @@ There are 6 pattern types:
14801495
"test":
14811496
print("Oh snap! It's a string!")
14821497

1483-
1484-
- Variable pattern
1485-
Matches the contents of a variable/enum::
1498+
- Expression pattern
1499+
Matches a constant expression, an identifier, or an attribute access (``A.B``)::
14861500

14871501
match typeof(x):
14881502
TYPE_FLOAT:
@@ -1492,7 +1506,6 @@ There are 6 pattern types:
14921506
TYPE_ARRAY:
14931507
print("array")
14941508

1495-
14961509
- Wildcard pattern
14971510
This pattern matches everything. It's written as a single underscore.
14981511

@@ -1506,7 +1519,6 @@ There are 6 pattern types:
15061519
_:
15071520
print("It's not 1 or 2. I don't care to be honest.")
15081521

1509-
15101522
- Binding pattern
15111523
A binding pattern introduces a new variable. Like the wildcard pattern, it matches everything - and also gives that value a name.
15121524
It's especially useful in array and dictionary patterns::
@@ -1519,7 +1531,6 @@ There are 6 pattern types:
15191531
var new_var:
15201532
print("It's not 1 or 2, it's ", new_var)
15211533

1522-
15231534
- Array pattern
15241535
Matches an array. Every single element of the array pattern is a pattern itself, so you can nest them.
15251536

@@ -1579,7 +1590,8 @@ There are 6 pattern types:
15791590
"Sword", "Splash potion", "Fist":
15801591
print("Yep, you've taken damage")
15811592

1582-
**Pattern guards**:
1593+
Pattern guards
1594+
""""""""""""""
15831595

15841596
Only one branch can be executed per ``match``. Once a branch is chosen, the rest are not checked.
15851597
If you want to use the same pattern for multiple branches or to prevent choosing a branch with too general pattern,
@@ -1602,6 +1614,7 @@ you can specify a guard expression after the list of patterns with the ``when``
16021614
- If there is no matching pattern for the current branch, the guard expression
16031615
is **not** evaluated and the patterns of the next branch are checked.
16041616
- If a matching pattern is found, the guard expression is evaluated.
1617+
16051618
- If it's true, then the body of the branch is executed and ``match`` ends.
16061619
- If it's false, then the patterns of the next branch are checked.
16071620

0 commit comments

Comments
 (0)