Strict typing issues and caveats #8090
Closed
SanctusAnimus
started this conversation in
Scripting
Replies: 1 comment 1 reply
-
Thanks for the proposal! Consolidating with #6941 and #7329, as these are prerequisites for enforced static typing to be usable more seamlessly. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Newest beta of Godot 4.2 release today, which contained new options to enforce types on everything (which i turned on immediately ofc cause there's many benefits to it)
However doing so revealed some flaws and caveats in both my codebase design and Godot API design in general - which i want to discuss.
Most big issues i've faced so far when fixing type errors in my sandbox project were mainly related to exports supporting multiple adjacent types, or physics, and i'll start with it.
Suppose a simple class extending
Area2D
, that seeks for other areas on specific layer, checks if they are Pickups and launches towards player. Code for it would look like thisNote the last 3 lines, with area entered processing, where i check for appropriate type. Strict typing here forces me to also "cast" it here, despite me already checking for it - which i'm not necessarily a fan of.
There's also
match typeof(x):
- which also just checks the type, without narrowing it on the variable itself, so you'll have to cast it anyway (unless i'm misunderstanding something)Is there a better way this could be written, or have alternative syntax?
It becomes even more severe when you use PhysicsServer directly for intersections - as they return untyped dictionary!
Take for example this partially untyped code snippet:
Most of this code doesn't play well with types - not only
collider
have no type (which could be many collision objects, that's fair), but other seemingly obvious fields likeposition
are untyped as well!While i really want to have everything typed, as it's much safer (and also faster!), having to spam
X as Y
is a bit silly.Moreover, that alone is also not an ideal solution, as it's considered an "Unsafe cast" - since original value has an unknown type.
I really do think that these places, among others which use Dictionaries as return types, as well as some signals, could use a better solution (like structs that are still being discussed in proposals).
Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions