Replies: 4 comments
-
My original proposal was to implement the behavior of instances of the same class performing math operations on each other. |
Beta Was this translation helpful? Give feedback.
-
Operator overloading is a form of hidden control flow, which makes a program harder to read. With operator overeloading, you don't know for certain if using a math operator is going to call a function or not (when you don't know the object's type). GDScript already has a fair amount of hidden control flow (such as set/get properties), but I think we should avoid adding more. |
Beta Was this translation helpful? Give feedback.
-
There are plenty of issues about implementing operator overloading. First it just makes dynamic dispatch more difficult (for operators). We could add version of This would work only for objects of the same type pretty much, or at least for only one combination of types if both implement the magic method (so Since GDScript does not have method overloads (and adding those is an extra complication) a possibility would be to have a registry of all possible overloads. You would have to make sure those are registered early enough. Then With this method, you would be able to add operator overloads for any pairs of classes, including adding more at runtime. This means that GDScript cannot know at compile time if an operator exists or not, much less what type it could return (since this all dynamic). Therefore it cannot optimize this behavior and can't show any error message in-editor even if the operation will never be valid. All in all, while it's technically not impossible, I don't think the cost is worth the gain. It would be extra effort to add indirections that would be used by a handful of libraries and would remove compile-time validations for everyone, just for the convenience of writing |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This isn't particularly useful, because apparently you can define your own math methods, like
func add(other: YourClass) -> YourClass:
,func greater_than(other: YourClass) -> bool:
.Also I don't know if there is any drawback of math operator overloading. I just thought it's convenient to have.
For example, I have my own NumAttribute class to hold any float/int gameplay attribute, with automatic clamping/rounding, signals upon any change, etc., which is good. However, performing math on different NumAttribute feels troublesome. If I could do
attr1 + atrr2
by overloading_add
, orattr1 > attr2
by overload_greather_than
(or_gte
), that would be cool.I'm curious why no one has proposed this in the last few years, do you all think operator overloading is unnecessary?
For reference:
Related to godotengine/godot#23488 (The proposer stated the proposal is only for
_str
overloading, which was already implemented as_to_string
.)Beta Was this translation helpful? Give feedback.
All reactions