Operator Capturing

I have a use-case (custom number-like objects) that would be made simpler if it were possible to override the behavior of the +, -, *, and /, similar to python's magic __add__, __sub__, __mul__, and __div__ functions. I was thinking of a property such that when something is, say, added to an object, object.operators.add is called.

Thoughts?

See: GitHub - tc39/proposal-operator-overloading - very early but not off the table.

This is something you can play with today in QuickJS which has operator overloading built-in (something very similar to, but not exactly the same, as overloading described in the proposal above).

2 Likes

Thanks! I'll look into it!