Changing operator overloading to be more like Python #44
waywardgeek
started this conversation in
Ideas
Replies: 1 comment 2 replies
-
Does it mean that the same calling a function with two differently typed arguments is not possible?
wouldn't be possible? |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm going to change operator overloading to be more like Python, now that I better understand why Python is the way it is.
The current operator overloading attempts to be more general than Python, but the current scheme causes problems. For example, currently, in Rune, I can write:
If Rune finds a built-in operator, it goes with that, but if it would otherwise be a compile-time error, it checks for overloaded operators, attempting to match each function until one matches. The function above always matches addition operators because it has no type constraints.
This turns out to be waaaaay to slow. Let's say we have an overloaded function like this:
Binding this recursively first must bind the type constraints. The old binder binds them left to right, so typeof(a) is valid as a constraint for b. This binding isn't done just once. Instead, it is done every time an addition operator is matched against this function. This significantly slows down the old binder.
In the new binder, we only bind things once, including type constraint expressions. In general, the new binder first looks up a function by its name and signature, and if the signature is new for that function, it makes a copy of the function, and binds the copy to the signature of the parameters passed in. Type constraints are bound in the context of the signature, so the following will be legal with the new binder:
This is fast because we first lookup the function being called, and verify we're calling it with a new signature, before doing an expensive function copy. For overloaded operators, using the old scheme, we would have to make a function copy and attempt to bind it for every overloaded operator function for the given operator. This is simply too slow.
The new scheme is like Python. Only one operator overload is allowed in any given scope. The function is uniquely identified by its name, so we can efficiently bind even though we make a copy of that function for each unique signature.
Like Python, we need right-hand-side operators, so I'm thinking of using keywords "operator" and "roperator".
Beta Was this translation helpful? Give feedback.
All reactions