operator overloading proposal

G. Kay Lee balancetraveller+es-discuss at gmail.com
Sun May 8 04:55:50 UTC 2016

I'd say it's way too early to ask for a champion on this because just a
quick skimming revealed a lot of places that didn't add up. For example,
the proposal suggested that overloading is primarily targeted at making it
easier to work with user-defined classes, but curiously a
`Function.defineOperator()` method is proposed instead of some syntax that
feels more tightly integrated with the class definition syntax.


class Point {
    constructor(x, y) {
        Object.assign(this, { x, y });

    toString() {
        return `(${this.x}, ${this.y})`;
Function.defineOperator('+', [Point, Point], (a, b) => new Point(a.x +
b.x, a.y + b.y));


The demo code made this flaw evident - it looks like a giant step backward
to define an instance method like this, don't you agree?

It's also apparent that the `@operator decorator` part of the proposal is
an effort trying to address this issue, but it really is not the
responsibility of the standard to try to define such a thing.

What I'd suggest is that perhaps you should rethink your proposed syntax
and redesign it to become an extension of the ES6 class definition syntax.

Another thing is that, IMHO, currently there are too much
quirks/conventions in the proposal that feel non-evident and non-flexible
which is destined to trip people over from time to time. It would be great
to make a proposal that's simple and don't include too much assumptions.

Finally, I'm not sure about the current status of macros, but last I heard
of it, they say it's going to make its way into the standard pretty soon
(TM), and macros can do much of the things overloading could, and much more.

On Sun, May 8, 2016 at 8:51 AM, Kevin Barabash <kevinb at khanacademy.org>

> I forgot to mention in my last email that I'm looking for a champion for
> this proposal.
> On Sat, May 7, 2016 at 5:24 PM, Kevin Barabash <kevinb at khanacademy.org>
> wrote:
>> Hi everyone,
>> I've been working on implementing operator overloading and would like to
>> submit a proposal.
>> I think operator overloading would be a useful addition to the language.
>> In particular I think it would be useful for defining operations on common
>> mathematical object types such as complex numbers, vectors, matrices, and
>> sets.
>> I've create a working prototype that consists of:
>>    - babel plugin that rewrites operators as function calls
>>    - a polyfill which defines these functions and which call the correct
>>    argument-specific function based on the arguments' prototypes
>>    - Function.defineOperator which can be used to define which function
>>    an operator should use for the specified types
>>    - "use overloading" directive which allows users to opt-in
>> More details can be found at
>> https://github.com/kevinbarabash/operator-overloading.
>> The babel plugin can be found at
>> https://github.com/kevinbarabash/babel-plugin-operator-overloading.
>> I also have a demo project at
>> https://github.com/kevinbarabash/operator-overloading-demo.
>> The design was inspired by some of the slides from
>> http://www.slideshare.net/BrendanEich/js-resp.
>> – Kevin
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160508/7669b3b1/attachment-0001.html>

More information about the es-discuss mailing list