How should operator overloading work?

# Coroutines (5 years ago)

How I imagined binary operator invocation would work is this:

Our example: a + b

If a or b is an object we try to look for an overloaded operator to call like so:

(a[Symbol('+')] || b[Symbol('+')])(a, b)

^ disregard that I'm assuming both a and b are objects I can index safely

Okay, so turns out neither a nor b have an overloaded operator for '+'. We default to calling the normal operator handler that coerces with valueOf() as necessary.

To me this seemed rather simple.. Operators would just be defined like: some_object[Symbol('~')] = function (lhs, rhs) { ... }

The only confusion I see is if we want to define explicitly the postfix or prefix form of unary operators like ++. I don't know what I'd call them as symbols.

I always saw overloading operators with Symbols, but you could just have reserved member names on the prototype of the object as well ~ '++operator' similar to C++ ?

# Andrea Giammarchi (5 years ago)

I should probably read other threads too about this, but is Symbol('+') a typo and it should be a property like any other well known Symbol so that it's actually Symbol['+'], Symbol['~'], and others? AFAIK invoking Symbol passing a string should create a named Symbol, and not a special one.

Thanks for any sort of clarification. Best

# Coroutines (5 years ago)

On Tue, Dec 29, 2015 at 11:15 AM, Andrea Giammarchi <andrea.giammarchi at gmail.com> wrote:

I should probably read other threads too about this, but is Symbol('+') a typo and it should be a property like any other well known Symbol so that it's actually Symbol['+'], Symbol['~'], and others? AFAIK invoking Symbol passing a string should create a named Symbol, and not a special one.

Thanks for any sort of clarification. Best

Yeah, I messed up there, should be Symbol['+'] simillar to Symbol.iterator

In my example, I have Symbol('+') everywhere which would mistakenly create several symbols with the same '+' description.

# 罗勇刚(Yonggang Luo) (5 years ago)

Does operator overloading works means BigInt and uint64_t & int64_t would works in Javascript?

# Coroutines (5 years ago)

---------- Forwarded message ---------- From: Coroutines <coroutines at gmail.com>

Date: Tue, Dec 29, 2015 at 1:49 PM Subject: Re: How should operator overloading work? To: luoyonggang at gmail.com

On Tue, Dec 29, 2015 at 11:34 AM, 罗勇刚(Yonggang Luo) <luoyonggang at gmail.com> wrote:

Does operator overloading works means BigInt and uint64_t & int64_t would works in Javascript?

I was not saying it currently works - I was asking how others think it should work :>

I made a thread to see what people would expect from operator overloading in ES7

# Nicolas B. Pierron (5 years ago)

Note, do we want to distinguish the binary operator Symbol['+'] from the unary operator Symbol['+']. Also, I am not sure how extendable the language is, especially if we are looking for operators like Symbol['>>=_'].

# Coroutines (5 years ago)

On Wed, Dec 30, 2015 at 8:31 AM, Nicolas B. Pierron <nicolas.b.pierron at mozilla.com> wrote:

Note, do we want to distinguish the binary operator Symbol['+'] from the unary operator Symbol['+']. Also, I am not sure how extendable the language is, especially if we are looking for operators like Symbol['>>=_'].

The "C++ way" of naming these would probably be nicer, but maybe something like this:

obj[Symbol['operator+']] = (lhs, rhs) => ...

obj[Symbol['++operator'] = (rhs) => ...

obj[Symbol['operator>>='] = (lhs, rhs) => ...

I saw that you wanted to say where the operator appears (if it's binary, unary, postfix, or prefix) depending on the _ - but certain operators (like >>=) can only be binary operators so there wouldn't be

any confusion unless we want to get into the land of defining custom operators like in Ruby. :-)