-
Notifications
You must be signed in to change notification settings - Fork 6
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
Useful to Node.js Core #11
Comments
Completely agree with the first para; the original bind operator as well as the proposal @js-choi plans to present at the next agenda also help address node’s primordials use case in the same way as this proposal, which is also my use case, and the reason i wanted to ensure bind wasn’t dead before pipeline advanced. |
@ljharb i think the bind operator changes the general flow and are bigger refactors from normal JS workflows and don't necessarily have the path towards dealing with protocols that this proposal does. |
As for the symbol, you could do: const iter = Function.call.bind(Array.prototype[Symbol.iterator]);
for (const x of o::iter()) { } without any scope expansion. |
I’m not clear on how all three of the bind operator proposals are any different wrt protocols, which are methods that need to be syntactically called with a temporary receiver the same way as string methods. |
@ljharb other bind proposals take an expression and bind the values within that expression. This proposal allows dispatch to match the normal receiver of an expression during invocation using a value not on the receiver. Other bind proposals do not allow lexical receivers in normal JS position and instead use currying. |
I think we have very different understandings then. At the least, the original one and the one intended to be on the next agenda allows the equivalent of |
For reference: The alternative proposal to which @ljharb refers to is js-choi/proposal-bind-operator. I plan to propose it at the next plenary. I have been engaging with @hax to see how much we can reconcile our proposals (although we haven’t been able to talk about it recently). I’m not sure what the concern with regards to protocols is, but talking about other proposals might be off-topic to this repository, so I opened tc39/proposal-call-this#4. |
@bmeck I think the use case could be easily solved by: const ::{slice} from Array
function copyArray(arr) {
return arr::slice();
} or // extract all methods from Array and convert to a frozen extension,
// so could use A as namespace for all array methods
const A = Extension.from(Array)
function copyArray(arr) {
return arr::A:slice();
} About symbol, because for (let x of y::A:values()) {
} or const ::{slice, values} from Array
for (let x of y::values()) {
} Maybe we need general syntax for symbols if we have some use cases which must rely on the original symbol keyed methods. For example: for (let x of y::A:[Symbol.iterator]()) {
} But I doubt symbol based behaviors are hard to be robust, for example |
|
@ljharb It does.
If there is truthy value, |
That’s only if Type is Object, which it’s not for a string primitive. I do see that you named the arg “obj” tho, so maybe you were only talking about when passing an object. In node core, startsWith would only be used for a string argument, for this reason. |
@ljharb But the defensive code do not know whether it's object or primitive ? Or at least need to check type before using. Anyway, even in this case it's ok, but essentially symbol means some protocol, and one method may rely on that symbol and call the symbol properties/methods on the argument, so it seems the symbol properties/methods are meant to be virtual/dynamic and hard to be ensured in some static way --- this is what I ask. |
Defensive code does extensive type-checking for the reasons mentioned :-) |
Oh, that's another reason we need strong brand check :-) |
After rethink of the symbol, I realize that the problem is not whether it's symbol or string, but whether the keyed methods are tend to be static or virtual (aka. leave for subclass to override). So either we need syntax for extract and invoke the symbol keyed methods/accessors, or we need convenient way to extract the special protocol implementation (see first-class protocol proposal) of a class into a extension, for example: But the static/dynamic invoking problem also lead us to another important use case of |
I don’t think we need any special-casing for the weird regex symbol behavior, or that Set/Map calls add/set; that’s just something people need to know. |
Could u say more about this? I don't get it. |
Constructing a Map or a subclass calls |
Oh NO.... Isn't it a spec bug? I can't understand why the constructor call |
Obviously I don't know it... and I can say at least 95%+ developers don't know it. 😢 |
Oh , I guess it's for subclasses... |
I'd be wary of reliance upon it, it has been brought up a couple times as something potentially needing to break to fix various other subclassing issues. |
Both true - it's for subclasses, but since "subclassing builtins in JS" has a wildly inconsistent pattern/model, and it'd be nice to clean it up, it's best not to rely on it - meaning, a Map/Set subclass should always define its own set/add, respectively. |
Node contains a lot of methods that are uncurry the value of
this
in order to safely obtain functionality like[0, 1].slice()
that is not subject to prototype pollution. This proposal would effectively have a lexical way to perform theuncurryThis
usage of those primordials.CC: @ljharb
This wouldn't cover all the use cases but is potentially a solution to an ongoing problem Node is facing regarding maintainability of parts written in JS needing to be robust. I wonder if we could expand this proposal just a little to intercept protocols like
Symbol.iterator
as well as that would greatly simplify parts of Node's internals and migrate towards something that looks like more idiomatic JS.The text was updated successfully, but these errors were encountered: