godot-nim / gdext-nim

Godot GDExtension binding for Nim
MIT License
27 stars 2 forks source link

remove proc `castTo` for usability #69

Open panno8M opened 6 days ago

panno8M commented 6 days ago

The current implementation defines the castTo function separately from the Nim built-in typecast.

Built-in typecasting works correctly for objects that have been constructed as derived classes from the beginning. Example:

var node2D = instantiate Node2D
node2D.name = "TestNode"
assert node2D.name == "TestNode"

var node  = Node node2D
assert node.name == "TestNode"

var node2Ddash = Node2D node
assert node2Ddash.name == "TestNode"

On the other hand, if the library constructs the class as a base class even though it is itself the result of upcasting a derived class, this operation is a run-time error. This sounds like a localized example, but it also occurs with Node.getNode, etc.

var control = Control self.getNode"Control" # getNode returns Node.
# => ERROR: invalid object conversion [ObjectConversionDefect]

var control = self.getNode"Control".castTo Control # works.

The castTo function is intended to make casting work in such examples, but this restriction is too unfriendly, especially if you are not familiar with this library. If it could be determined at instance binding construct time what type this class actually is, the above problem could be avoided and the castTo function could be eliminated.