Skip to content

Commit c6f6b48

Browse files
authored
Merge pull request #7828 from Calinou/static-typing-for-loop
2 parents 746e02b + 8e4f72f commit c6f6b48

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

tutorials/scripting/gdscript/static_typing.rst

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ or write code like you always did!
1515
Static types can be used on variables, constants, functions, parameters,
1616
and return types.
1717

18-
.. note::
19-
20-
Typed GDScript is available since Godot 3.1.
21-
2218
A brief look at static typing
2319
-----------------------------
2420

@@ -271,6 +267,18 @@ Nested array types are not supported.
271267
var s: String = scores[0]
272268
scores[0] = "lots"
273269

270+
Since Godot 4.2, you can also specify a type for the loop variable in a ``for`` loop.
271+
For instance, you can write:
272+
273+
::
274+
275+
var names = ["John", "Marta", "Samantha", "Jimmy"]
276+
for name: String in names:
277+
pass
278+
279+
The array will remain untyped, but the ``name`` variable within the ``for`` loop
280+
will always be of String type.
281+
274282
Typed or dynamic: stick to one style
275283
------------------------------------
276284

@@ -350,33 +358,22 @@ Warning system
350358
Documentation about the GDScript warning system has been moved to
351359
:ref:`doc_gdscript_warning_system`.
352360

353-
Cases where you can't specify types
354-
-----------------------------------
361+
A case where you can't specify types
362+
------------------------------------
355363

356-
To wrap up this introduction, let's cover a few cases where you can't
357-
use type hints. All the examples below **will trigger errors**.
364+
To wrap up this introduction, let's mention a case where you can't
365+
use type hints. This will trigger a **syntax error**.
358366

359-
You can't specify the type of individual members in an array. This will
360-
give you an error:
367+
You can't specify the type of individual members in an array:
361368

362369
::
363370

364371
var enemies: Array = [$Goblin: Enemy, $Zombie: Enemy]
365372

366-
You can't force the assignment of types in a ``for`` loop, as each
367-
element the ``for`` keyword loops over already has a different type. So you
368-
**cannot** write:
369-
370-
::
371-
372-
var names = ["John", "Marta", "Samantha", "Jimmy"]
373-
for name: String in names:
374-
pass
375-
376373
Summary
377374
-------
378375

379-
Typed GDScript is a powerful tool. Available as of version 3.1 of Godot, it
380-
helps you write more structured code, avoid common errors, and
381-
create scalable systems. In the future, static types will also bring you
382-
a nice performance boost thanks to upcoming compiler optimizations.
376+
Typed GDScript is a powerful tool. It helps you write more structured code,
377+
avoid common errors, and create scalable systems. In the future, static types
378+
will also bring you a nice performance boost thanks to upcoming compiler
379+
optimizations.

0 commit comments

Comments
 (0)