Times are different these days ... 'cause the whole WASM premise was to enable any PL to land in the JS kingdom, and that's what I do daily ... but also that's what happens daily regardless of what I do:
If there is one crucial and missing interoperability shenanigans between JS and many other PLs is that JS new
syntax/keyword is rarely welcomed elsewhere or it means something completely different ... so that I don't know how many times this topic has been raised already, but I think it's really time to consider the smallest improvement that would improve PLs interoperability with JS by far:
Function.prototype.new = function () {
return Reflect.construct(this, arguments);
};
That's it, that's the proposal, because when I see stuff like this (see latest example) I feel sick inside:
import pythonmonkey as pm
my_date = pm.new(pm.Date)(0)
print(my_date)
There's clearly a need to disambiguate JS classes from regular invoke-able functions in JS, not really for the JS world where these can throw happily ever after, but for all places where the intent is inevitably ambiguous.
I had this conversations with various developers from foreign PLs and agreed with all of them: new
is a curse for interoperability and clarity, and the proposed function looks like the least JS can do to help disambiguating intents elsewhere.
Thanks for considering this proposal, the only alternative I could think about is about the ability to map that, hence auto-bind it when accessed, as in:
Object.defineProperty(Function.prototype, 'new', {
configurable: true,
get() {
return (...args) => new this(...args);
}
});
// mapping ...
['me', 'you', 'everybody', 'everybody'].map(User.new);
I don't think there's anything else really to consider or discuss, as I can't think of any other usage of the new
keyword out of a Function.protoype
... moreover:
- would it throw for arrows? sure, as
new () => {}
does already - would it throw for short-hand methods? see previous point
- would it make instantiation confusing? I don't think so ... it's been adopted already by foreign PLs when writing code in their language wants to understand if an instance is being created or not (also proxy traps relevant)
That's it, happy to clarify, if needed, anything else.
edit see wasmoon shenanigans to guarantee a JS class can be instantiated, using create
instead of new
because they (I assume) got inspiration from the only Object.create
case we have in JS these days ... all those create
tests could disappear if we had new
backed in, but that's not the only WASM runtime I've seen disambiguating ...