-
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
Function stubs? #3093
Comments
We used to have an arbitrary-object binding operator in CoffeeScript, but it was removed because it wasn't generally terribly useful. In what real-world cases would this be? Explain how those uses are worth the conceptual complexity of the new syntax. |
I think it's essentially only really applicable when passing a method into a higher-order function instead of calling the method directly. One (fairly common?) example of doing that from my personal experience is when using asynchronous methods with promises: # with Function::bind (which assumes that Function::bind is available on this platform!):
Q.nfcall obj.method.bind(obj), argumentOne, argumentTwo
# with function stub:
Q.nfcall obj=>method, argumentOne, argumentTwo |
In CoffeeScript, you'd normally define that method as a
Anything else? |
Well, that works if you assume all objects are being defined by you and in CoffeeScript, and not being received from external libraries. The latter is overwhelmingly more common in cases where you'd want to decorate the function, I suspect. (If you're defining the class directly, it can just use promises right off the bat and therefore doesn't even need |
I'm afraid you're going to have to think of a more compelling example, then. CoffeeScript shouldn't be adding new syntax and language features that are only helpful when working with external code -- they should be rightly useful when working with CoffeeScript itself. In that sense, this proposal feels like a variant of the old bind operator, which I'd encourage you to loop up in the archives. |
Function stub syntax would also be applicable to actual CoffeeScript-'native' objects in situations where the original method wasn't defined using As for the question of external libraries, I'm not sure avoiding features that aid JavaScript interop is the best plan, since it's an obvious reality that CoffeeScript code must interoperate with some amount of JavaScript. Automatic binding of natively-defined methods with |
Using It feels like this kind of syntax offers far more flexibility and conceptual simplicity than bound instance methods. The biggest use-case for me, mostly writing client-side code, is for writing event handlers e.g.: class MyClass
constructor: ->
@on 'event1', => @handle_event1 arguments...
@on 'event2', => @handle_event2 arguments...
# or
constructor: ->
@on 'event1', @=>handle_event1
@on 'event2', @=>handle_event2
... Naturally, you could wrap that up in a little I'm not sure about the syntax, but I like the idea. |
@connec -- you should also check out the previous "bind" operator. CoffeeScript's fat arrow is way less flexible ... but it turns out that in practice, if Fat arrows mean that you only have to bind once to the lexical this, and then you can forget about it -- as opposed to having to bind the function every time you reference or use it. Finally, in most modern JS platforms, |
I understand your argument for 'lexicality', and that's definitely a way around the problem, but it still feels like such a common idiom, and supporting common, simple patterns with concise syntax seems like a nice idea. Passing The fat arrow also moves this consideration to the definition site, rather than the call site. You could call this a 'good thing™' but Javascript's semantics make it hard to believe that Having a syntax for this would make it much easier to see what was going on, and even if the pattern isn't used by everyone every day it's nice to have an explicit and unified (and concise) way of expressing 'I want to call this object's method in its context'. |
In MoonScript, it's possible to get a reference to an object method that remains bound to its original object. This feature is called a "function stub" and works like this:
Given the way JavaScript's objects work and that
Function::bind
isn't reliably available on all browsers, a similar functionality in CoffeeScript would be valuable.We don't use
\
to denote method calls in CoffeeScript, so simply copying MoonScript's notation for the concept wouldn't work so well; perhaps reusing the bound-function symbol=>
in the contexts we use.
would, like so?Would compile to:
The above CoffeeScript is currently a syntax error, so adding this use for
=>
shouldn't break backwards-compat with anything. I'm open to other operator suggestions, of course.The text was updated successfully, but these errors were encountered: