tc39/proposal-operator-overloading

API doesn't work with class private member proposal

Opened this issue · 3 comments

The current API, from the README:

const operators = Operators(operatorDefinitions [, leftOrRightOperatorDefinitions...])
class MyClass extends operators { /* ... */ }
Object.preventExtensions(MyClass);

Using this API is a step backwards from recent class proposals, such as the private member proposal.

The Decimal example accessed a property called ._big, yet we would like the property to actually be private, so we would want to use .#big, yet the current API forces us to declare the operations outside of the class.

Furthermore, some operators really don't need to be reused on multiple classes, and are intended to be used on a single class, for these cases, I'd support (and really prefer) declarations that are syntactically inside of the class.

If it were private, it couldn't be used by code outside the lexical scope where the property was declared. How would that work?

@ljharb If the operation declarations were intended to be only used for instances of the specific class, and if the declarations were

...syntactically inside of the class...
then they should be lexically scoped to the class' body too.

I'm not sure what the possible API could be, maybe a decorator approach?

class T {
     #contents;

     @Operator
     "+"(a, b) {
        // Access private member here
    }

    ...
}

With the current decorator proposal something like that would be completely impossible.