Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typed loop variable example in for-loop section #9388

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Muller-Castro
Copy link
Contributor

No description provided.

@AThousandShips AThousandShips requested a review from a team May 17, 2024 08:25
@AThousandShips AThousandShips added enhancement topic:gdscript area:manual Issues and PRs related to the Manual/Tutorials section of the documentation labels May 17, 2024
Comment on lines 1551 to 1552
var arr: Array[String] = ["a", "b", "c"]
for s: String in arr: # Typed loop variable.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct, but the type is redundant here: it is inferred since the array is typed. The main benefit of this feature is that it allows you to downcast the element type, like for control: Control in get_children(). Also, this is useful for dictionaries that do not currently support typing.

Suggested change
var arr: Array[String] = ["a", "b", "c"]
for s: String in arr: # Typed loop variable.
var arr: Array = ["a", "b", "c"]
for s: String in arr: # Typed loop variable.

It's already documented Static typing in GDScript:

Since Godot 4.2, you can also specify a type for the loop variable in a ``for`` loop.
For instance, you can write::
var names = ["John", "Marta", "Samantha", "Jimmy"]
for name: String in names:
pass
The array will remain untyped, but the ``name`` variable within the ``for`` loop
will always be of ``String`` type.

Could we just duplicate this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:manual Issues and PRs related to the Manual/Tutorials section of the documentation enhancement topic:gdscript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants