No operator overloading for native types (?) #9611
Replies: 1 comment
-
Note that GDScript supports dynamic typing, so this cannot be done only at the static analysis level. In the case of untyped code, the performance penalty can be quite large. See:
As for the You are trying to do something that the current binding system simply does not provide. This may seem like a good idea for consistency, but the Godot API is not designed to be fully compatible with type systems and all the features of languages like C++ and C#. The Godot API is very pragmatic, focused on solving immediate practical problems and has little concern for elegance. |
Beta Was this translation helpful? Give feedback.
-
I am working on a change to BitMaps where you can use boolean operators. Internally, I have the functionality all working for the operator overloading I want, but the functionality in GDScript in editor is not there. I did some digging around in the codebase and ended up finding what was happening.
To check that a given binary operator is valid, it goes as follows:
When you pass in two Object types (i.e. most types in Godot), there is no handling for it, so it will always fail. This is frustrating because I have the functionality in C++, but have no way of converting it to GDScript without turning it into a method (which is what I want to avoid in the first place).
My idea is along the lines of this:
This should also probably be extended to the unary operators, since those are also common operators to overload.
This is only for the parser to find that there are errors. There will also need to be functionality in wherever the code is actually being run to access the operator behavior, but I'm pretty sure that it shouldn't be terribly difficult, considering how there this is essentially a subcase of how methods work, just with a different parsing situation. That being said, I'm also very new to Godot engine development, so I'm sure there are technicalities which can make this very difficult. It might be necessary to simply have methods defined in C++ binded as operators rather than trying to fit the operator overloading definitions into the existing binding code.
I think this would be a good feature to have in the engine because it would mean we could have less hard-coding when we need to expand upon the engine in the future. There are already several hundred lines of code in variant_op.cpp which define all the valid operators between any two given built-in types, and adding more built-in types is going to make it all the less managable. It would be a lot better to make it more modular in the first place. But more than that, I think it would be good to let native types have overloaded operators. It would make sense for many math-adjacent classes like BitMap which aren't useful enough to be a built-in type but have enough math where it makes sense to have an overloaded operator.
Beta Was this translation helpful? Give feedback.
All reactions