#Valid function signature is raised as invalid?

11 messages · Page 1 of 1 (latest)

limpid arrow
#

I have 2 resources, a parent and a child
ResourceA

extends Resource
class_name ResourceA

var foo := 42

ResourceB

extends ResourceA
class_name ResourceB

var bar := 69

ResourceB should be - to an extent - interchangeable as a type with ResourceA, because it inherits from it. However, this doesn't seem to work in this case:
ClassA

extends Node3D
class_name ClassA


func init(p_resource: ResourceA) -> ClassA:
    return self

ClassB

extends ClassA
class_name ClassB


func init(p_resource: ResourceB) -> ClassB:
    return self

ClassB's init function is registered as an invalid function signature:

#

Am I missing something, or is this a parser bug?

#

I'm using Godot 4.0 Beta2

stray delta
#

It's probably because GDScript doesn't support function overloading (multiple functions with the same name but different parameters), and it sees these init functions as being overloaded even though their types are related.

#

(a lot of languages don't like this business of elevating the type of a parameter in an overridden function)

limpid arrow
#

Hmm, that could be problematic then.
Are there any workarounds you would suggest?
The reason I found this in my game is because I have a VehicleGamePiece class that inherits from a GamePiece class, and they have matching VehicleGamePieceData and GamePieceData resources. And I'm trying to initialize them with those resources.

stray delta
#

The usual solution with GDScript is just to be less particular about types. For example, let ClassB accept a ResourceA object, and check that it's correct at runtime if necessary.

#

If you want a much stronger type system, it'd be better to look at C# or similar.

limpid arrow
#

Ahh, understandable.

#

Thank you for your help!