-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Sugar for function binding, uncurrying this
and setting this
context
#2136
Comments
Whoa. We've had an explicit bind operator in the past ( What if |
Oh, wait ... just thought of one. This wouldn't play very nicely with function literals -- at least without making a special case...
... hideous. |
So basically:
|
I like this idea, mostly because it unifies binding and call/apply with a single syntax. Just wanted to point out that a binding/currying operator can achieve a similar thing... The operator in that proposal, currently
Becomes:
We bind by currying
Becomes:
So we can write the binding examples above...
As:
Becoming:
There's a bit more to the operator, such as left-associativity, so if you're interested I recommend checking it out. |
@jashkenas Definitely not worth the complexity and obscurity. If a function is intended to be used by multiple objects, then it should stand on its own.
Of course, I understand you might be dealing with third-party code that implements a nice algorithm, and the original authors might not have understood the wisdom of decoupling algorithms from objects. It would be nice to see a few more concrete cases where that technique actually comes up. It seems like calling forEach and trim for foreign objects would be brittle to begin with. |
Treating the And yes, it doesn't play very nicely with function literals, but I don't think that'll be very common. you can just use the context directly in the function body or the fat-arrow when binding to I know some of this can be achieved with the partial application operator (except for calling directly with the context) - but I find it much less readable than a dedicated operator, having I do agree with your point, and I personally prefer functional-style code over OOP-style, so I would hardly ever use it to call my own functions. But, as you said, not everyone agrees with that and a lot of libraries (and native functions, mostly Array methods) are exposing functions that expect to be run on directly from an object, but are also useful as a standalone functions that could run on other objects too. Also, I have lots of higher-order functions that delegate to functions passed as arguments (like in |
Or |
@shesek I guess I just think it's already easy enough to decouple functions from "this"-ness with the current syntax. All you need is a generic high-order function called "this_free" (or perhaps some better name).
Even if you like the sugar, it's worth asking how often the feature would get used. Ultimately, the syntax would only come up when people normally use "call" and "apply", which I'm guessing is pretty infrequent in most codebases. Also, the sugar doesn't reduce line count, so I'm not seeing a big win in expressiveness. |
@satyr @showell IMHO setting a different context is better expressed using a dedicated operator, as it should stand-out as a special operation rather than yet another function call. I also find that syntax more natural for the developer to write and for his co-workers to read and understand. That being said, expressiveness mostly boils down to personal preference and coding style, so there isn't really right and wrong here. BTW, |
@shesek I known that call/apply are fairly common in JavaScript projects, but I was wondering about CoffeeScript projects. An interesting discussion example might be underscore.coffee, which uses "apply" pretty frequently (grep output below):
How do you sugar |
Now that I think about it, to be honest - those numbers aren't very accurate as splats replaces a lot of the cases where
(assuming you meant |
So, closing this one for pretty much the same reasons we removed
|
@jashkenas This only takes into account the binding use case (which, as I said, isn't very useful for function literals anyhow), but I think that applying with context (as an alternative to |
|
@jashkenas: Though, that requires that |
@jashkenas, in same time our competitors are using this: https://github.com/tc39/proposal-bind-operator I think we need reinvent bind operator again. |
Somewhat based on a syntax proposed on es-discuss, I would like to suggest the following additions:
Setting the
this
context for function applications:Binding functions to objects:
Possibly even
func@@
as sugar forfunc@this
? Could be useful as a fat-arrow version for non-literal functions.Uncurrying
this
: (as proposed on es-discuss)For complex expressions on the RHS or LHS of the
@
, parentheses should be used, e.g.func@(foo bar)
,(baz qux)@obj
,(baz qux)@(foo bar)
The text was updated successfully, but these errors were encountered: