-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[IDEA] operator overloading via Proxy traps #54
Comments
This sounds like a nice idea on paper to follow existing practices but in reality this falls apart really bad with a number of flaws:
function Vector(x, y) {
if (!new.target) throw new TypeError(`Class constructor ${this.name} cannot be invoked without 'new'`);
this.x = x;
this.y = y;
}
Vector.prototype = new Proxy(Vector.prototype, {}); The code doesn't error anymore, great, but we still can't make
Let's ignore for a second all the problems with even getting it to work and assume we do get it to work somehow without violating spec or breaking changes. Plenty of problems remain...
And of all things, we'd be proxying a prototype, an object that is highly accessed and checked against from all sorts of places in the language semantics like inheritance, constructors, any kind of property access that doesn't match an own property, etc. It'd be a total widespread performance disaster.
Even if we pretend further, that we somehow find a way for proxies to not have such absurd performance drains, we still have many more smaller but not insignificant issues left... simply too many to continue on listing here, I hope what I've listed so far has been enough to make anyone see how terrible of an idea it would be to proxy prototypes. In an ending note, I do share the wishful intent here for integrating with existing language semantics rather than coming up with brand new extra things to memorize, proxies are just the wrong call, instead, how about we consider well-known symbols or decorators instead? Although personally I'm also pretty fond of the primary syntax suggested in the linked issue itself despite being new syntax. |
There is already the possibility to trap certain operators on objects and similar (e.g.
()
ornew
) by using Proxy traps. For consistency it would seem sensible to put the overloading of other operators there as well.It could work like this:
The "sugared" version is to be defined.
This would of course bring some further changes to the proposal, most notably that the overloaded operators would be inherited and always be present as there is no "with" to explicitly enable them. However, that would probably not be a problem as it is in line with how Proxies (and thus our currently limited operator overloading) works.
The text was updated successfully, but these errors were encountered: