-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support a very basic form of operator overloading #1520
Comments
I am a big fan of this general idea. I think we should use generic types where it makes sense: in particular the deref interface should be something like
would allow
you could then write However, I think the fact that one must implement all I guess it depends a bit on whether we intend for these operators to be used strictly for their analogous purposes or whether we intend to allow nicer notation for a few other cases. I say intend because people will use them for other things beyond numerics and lists whatever we do, but I guess we can make it more painful for them if we want to. |
I guess this is a matter of taste. I like operators to mean one thing and only one thing -- I think Haskell's |
This issue sorta relates to Issue #535. |
Actually I was reading the summary for 535 way too fast, it was about overloading in general and not operator overloading. In any case, I believe that if Rust is going to support ad-hoc operator overloading, it should also allow defining new operators. Otherwise the existing operators will be abused to do things that have nothing to do with the original operator's meaning; for example the |
When no built-in interpretation is found for one of the operators mentioned below, the typechecker will try to turn it into a method call with the name written next to it. For binary operators, the method will be called on the LHS with the RHS as only parameter. Binary: + op_add - op_sub * op_mul / op_div % op_rem & op_and | op_or ^ op_xor << op_shift_left >> op_shift_right >>> op_ashift_right Unary: - op_neg ! op_not Overloading of the indexing ([]) operator isn't finished yet. Issue #1520
The method `op_index` (which takes a single argument) is used for this. Issue #1520
It's less likely to clash with something than 'neg'. Issue #1520
(RFC)
We could add interfaces
core::num
andcore::deref
, with the first implementing methodsadd
,sub
,mult
,div
,rem
,neg
, and probably a few more, and the second onlyderef
. The compiler can then resolve operators+
,-
,*
,/
,%
, and[x]
on types that are not built-in numerics or vectors as if they were calls to the corresponding method (1 + 1
=>1.add(1)
), and numeric-style and sequence-style user types can implement these interface to allow the operators to be applied to them.This is not the most elegant solution (
1 + 1
is more likeadd(1, 1)
than1.add(1)
), but it would give us operator overloading on the short term, without first needing a lot of extra interface features.We will need some kind of support for a 'self type' in interface declarations to be able to say that
add
takes an returns a value of the type that the interface was specialized on, which is not currently expressable.The text was updated successfully, but these errors were encountered: