tc39/proposal-operator-overloading

<=> starship operator for unified overloading of comparison operators

c69 opened this issue ยท 4 comments

c69 commented

Following the same logic that led to its inclusion into C++20: https://devblogs.microsoft.com/cppblog/simplify-your-code-with-rocket-science-c20s-spaceship-operator/

  • <=> three-way comparison is a way to define all overloaded comparators in the same place.
  • operator <=> (left, right) { return a < b ? -1 ? a > b ? 1 : 0; } // syntax is sttawman
  • this operator makes most sense during definition, and not as a standalone, so: if (a <=> b) is still illegal

The spaceship operator would be a different proposal; if that proposal existed, it would have to figure out how it interacted with operator overloading as part of figuring out cross-cutting concerns within stage 1.

The current readme states "The definition of >, <= and >= is derived from <".
I think it would be quite nice if the Operators function would also accept an object with a "<=>"(a, b) method instead of a "<"(a, b) method, with the same interface as a .sort() comparison function.
We shouldn't introduce <=> itself as new syntax in JS in this proposal.

As an alternative to this, implicitly overloaded operators by the JS engine itself: #53