godot: Incorrect type checking
Godot version
3.5
System information
Windows 10
Issue description
According to my understanding, the point of specifying variable types is to allow compiler-time checks even though Godot does not have static typing at run-time. However, when dealing with class hierarchies, Godot does not handle typed variables as expected. If we declare a variable to be an instance of class C, Godot compiler will allow the access to properties and functions that are not defined in given class C, but in its subclasses, which is incorrect.
It has to be fixed so that this code does not compile:
class_name Parent
func parentFunction():
print("parent")
...
class_name Child
extends Parent
func childFunction():
print("child")
...
var a: Parent = Child.new()
a.childFunction() # this should not compile, because class Parent does not have childFunction
Steps to reproduce
- define a class
Parent - define a class
Child, extendingParent - define a function
min classChild, such that it does not exist inParent - define variable
aof typeParent - initialize variable
awith an instance of classChild - try to call function
m, which exists in Child but not in Parent - this code should not compile, because it tries to call a function which is not defined in the type of variable
a
Minimal reproduction project
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 20 (12 by maintainers)
Static typing is optional in GDScript. In particular, the following code is allowed by default, although it is not type-safe:
If you want strict checks, I wrote above how to enable them. See also this comment.
That is not what it does.
GDScript is not statically typed. And it is interpreted. The type hints exist to allow the Editor to provide you with useful information. There is no way to prevent you from writing code that ignores the hints. You can save a script with a type error and run it just fine so long as you don’t run into an actual execution error.
If you think of GDScript as being in the same class of language as JavaScript, your expectations may be more in line with how things work. Or TypeScript since you are using the optional type hints.
Point is. It will execute without error. So the editor, is not giving you an error as it stands right now. I am plainly explaining why things work how they do, and what frame of reference allows for this to be acceptable.
It probably should error. I will reiterate.
And I will reiterate. That we (you) should investigate under which circumstances it is not providing an error, even though we believe that it should.
You can enable warnings:
Additionally, you can treat all warnings as errors:
In 4.0-dev you can set this up on a per-type basis: