mozilla/rust — Issue #1520: Support a very basic form of operator overloading
Author: marijnh
Created: 2012-01-14 11:56:27+00:00
State: open
Labels: rfc

(RFC)

We could add interfaces `core::num` and `core::deref`, with the first implementing methods `add`, `sub`, `mult`, `div`, `rem`, `neg`, and probably a few more, and the second only `deref`. 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 like `add(1, 1)` than `1.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.