Godot-proposals: Add static typing for loop variables in `for` loops

Created on 25 Mar 2020  路  7Comments  路  Source: godotengine/godot-proposals

Describe the project you are working on:

A Zachtronics style puzzle game, using static typing as much as possible.

Describe the problem or limitation you are having in your project:

My code does a lot of work with Arrays, which contain objects that are all the same type. This is extremely common with arrays in general; I've rarely seen use cases for arrays containing completely arbitrary types. There's almost always at least a common base type for all elements.

When looping over the Array with a for loop, the docs explicitly state that you can't specify a type. This is forbidden:

for name: String in names:
    ...

So inside the loop body, name will be untyped, even if we _know_ we started out with an array of Strings. We'd prefer to fail with a clear error if that assumption is ever violated.

The reason, according to the docs, is:

You can鈥檛 force the assignment of types in a for loop, as each element the for keyword loops over already has a different type.

That's a nonsensical argument; in other places (e.g. simple variable assignments) it's entirely possible assign values of unknown types to variables that have a static type:

var foo = 0
var bar: String = foo  # Fine at compile time, error at runtime.

Describe the feature / enhancement and how it helps to overcome the problem or limitation:

Allow syntax like the above:

for name: String in names:
    ...

It would check the type at runtime and throw the usual error if it doesn't match:

Trying to assign a value of type 'whatever' to a variable of type 'String'.

Within the body of the for loop, name would have the type String and provide autocompletion and other static type checks accordingly.

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:

I would hope that it's fairly straightforward to add this to GDScript, but I've never looked at that part of the codebase before so I might be wrong.

If this enhancement will not be used often, can it be worked around with a few lines of script?:

One workaround is to live with a dynamically typed loop variable, at the cost of losing autocompletion and type checking in the editor.

The other workaround is to use a helper variable and cast it explicitly:

for temp_name in names:
    var name := temp_name as String
    ...

Is there a reason why this should be core and not an add-on in the asset library?:

It's a core GDScript feature, so it cannot be implemented externally.

gdscript

Most helpful comment

@Deftwun That syntax doesn't seem as readable as for name: String in names. Also, as has different semantics from : type hinting in GDScript.

All 7 comments

Godot noob & Just my 2 cents but this syntax would be awesome to get type hints inside for loops

for name in names as String:
    print(name)

I'd expect it to throw an error at runtime if types didn't match

@Deftwun That syntax doesn't seem as readable as for name: String in names. Also, as has different semantics from : type hinting in GDScript.

I would also expect it to follow what it's doing currently.

for name : String in names:

Is there any progress on this proposal? Given that we can't use type hints with arrays (though the pooled arrays exist for some types) being able to specify what type we expect to be looping through would be helpful in terms of editor auto-suggestions.

What's the plan for this proposal?
It would be a really valuable Quality of life improvement

4.0, AFAIK, will get typed arrays and dictionaries, so, bearing that in mind, is it necessary to specify the type of the loop variable when it can be inferred (at compile time!) from the container items' type? Not against this proposal though, just a thought.

@katoneko Typed (Variant) arrays are planned for 4.0, but I'm not sure if typed dictionaries will also be added in 4.0 as they are more difficult to implement.

Was this page helpful?
0 / 5 - 0 ratings