-
Notifications
You must be signed in to change notification settings - Fork 25
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
Consider adding placeholder for receiver binding #23
Comments
Will it be OK to use a token containing If optional chaining is switching to |
I'd rather stay away from As far as using |
I don't understand the similarity concern. I could see avoiding I agree that using another token like |
Besides, I'm likely to change the grammar above to include a single-step optional chain as well, e.g.: ?this??.prop()
?this??[expr]() Yeah, that's a lot of |
It could also be something as simple as |
Although, I'm less likely to consider adding support for optional chaining into this proposal if we eventually have a syntax for optional pipelines (e.g. |
Another option would be to change the placeholder to not be |
I don't particularly like I suppose that if optional chaining is using
|
?this
placeholder(?)
placeholder for receiver binding
(?)
placeholder for receiver binding
@rbuckton If it was to be implemented then I would expect the following syntaxes to also work or at least be considered in the future (using #.prop === (x) => x.prop // property access
#?.prop === (x) => x?.prop // optional property access
#(arg) === (x) => x(arg) // call
#?.(arg) === (x) => x?.(arg) // optional call
#.method() === (x) => x.method() // method call (this proposal)
#?.method() === (x) => x?.method() // optional method call
#.method?.() === (x) => x.method?.() // method with optional call
// etc So using |
I don't know if it's feasible, but another option is to not have any symbol at all: const friendsList =
getData('users')
|> JSON.parse
|> .filter(.friends.includes(currentUser))
|> .map(.name)
|> unique
|> .join(', ')
// same as
const friendList =
getData('users')
|> JSON.parse
|> users => users.filter(user => user.friends.includes(currentUser))
|> users => users.map(user => user.name)
|> unique
|> users => users.join(', ') |
@phaux that wouldn't be feasible, because dot access and bracket access must both work, and |
|
@rbuckton you can't put an expression in the pipeline? |
According to latest node.js:
but
so it could work as long as the property names aren't numeric. |
and for computed property names it would be
I'm not entirely sure if it's worth treating dot+numbers versus dot+identifier so much differently. I'm just throwing out ideas. |
leading |
Given the syntax and semantics introduced in #49, and the advancement of Hack-style pipelines to Stage 2, I no longer believe introducing a placeholder for the receiver binding is an avenue to pursue. This suggestion was initially driven by a demand to support piping a callee in F#-style pipelines: [1, 2, 3] |> ?.map(x => x + 1); However, Hack-style pipelines do not require this capability, as In #49, we introduced the prefix token
Both the smart-mix and Hack-style pipeline proposals were considering a prefix token like
However, in both cases these expressions are lazily evaluated and semantically the same as A prefix token that comes before the callee is not conducive to eager evaluation, as it can introduce confusion as to which part of a more complex expression is to be partially applied:
To suit eager evaluation, the above would necessarily be a syntax error since the first call expression we encounter is We chose
This change also means that only arguments can be partially applied, since the prefix applies to the argument list and not the callee. Since the callee is not an argument, it cannot be partially applied. Instead, you have two alternatives if you need to "partially apply" the callee: arrow functions and utility functions. As mentioned above, arrow functions are lazily evaluated. They introduce a closure over the outer environment record, which means that they will observe state changes to closed-over variable bindings. In addition, the body of an arrow function is repeatedly evaluated each time it is called, meaning that any side effects within the body of the arrow can be observed. If your code is structured in such a way that there are no mutations to closed over variables and the arrow body does not contain side effects, then an arrow function is a perfectly acceptable solution to providing a partially-applied callee. If eager-evaluation semantics are still necessary, however, its fairly easy to write utility functions that can support partial application of a callee: const call = (callee, ...args) => callee(...args);
const invoke = (receiver, key, ...args) => receiver[key](...args);
const callWith1And2 = call~(?, 1, 2);
const invokeSayHello = invoke~(?, "sayHello"); |
So in a Hack pipeline, |
If such helpers seem useful, especially with respect to partial application, I suggest you file an issue on https://github.com/js-choi/proposal-function-helpers for their inclusion (although |
Did you mean Pipelines are still evaluated left-to-right, so in |
oops, yes, i did mean |
For example: const bob = {
name: "Bob",
sayHelloTo(name) {
console.log(`Hello ${name}, I'm ${this.name}!`);
}
};
const sayHello = bob |> ^.sayHelloTo~(?);
sayHello("Alice"); // prints: Hello Alice, I'm Bob! |
This proposal extends CallExpression to allow
?this.prop()
or?this[expr]()
as a way to define a placeholder argument for a partially applied CallMemberExpression. Both?this
and?
could be combined in the same expression, in which case the?this
placeholder argument will be the first argument in the partially applied function result.Syntax
Grammar
Examples
The following show approximate syntactic transformations that emulate the proposed semantics:
It is not possible to reference
?this
on its own in an argument list.Alignment with the pipeline operator (
|>
)When combined with the pipeline operator proposal, this feature could allow the following (based on original example in https://github.com/tc39/proposal-pipeline-operator/wiki):
Alignment with other proposals
This syntax aligns with the possible future syntax for positional placeholders (e.g.
?0
,?1
) as proposed in #5. If both proposals are adopted, then when using?this
all positional placeholders are offset by 1 with respect to their actual ordinal position in the argument list (similar toFunction.prototype.call
). For example:The text was updated successfully, but these errors were encountered: