-
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
Improve do
(x = y) -> and support
do (x) ->`
#960
Comments
Yo Trevor, instead of opening tickets as this stuff evolves, mind dropping by |
I'm afraid that |
Now that Currently,
instead of the clearly more efficient (and, I would argue, more readable)
Also, one additional tweak:
|
I actually just came here to create exactly this ticket. As far as I can tell, Coco does what Trevor is suggesting, and it would definitely be helpful to me. One use case is when the value you want to bind to the anonymous function isn't bound in the current context, e.g.
What I want to say here is really:
And I want it to compile to:
But it currently compiles to:
Note that this can even cause an unintuitive ReferenceError! |
Whoops, forgot to add that with CoffeeScript's current implementation I am forced to bind |
Still hoping to see these
that inner |
Update: The State of
|
Here's another problem with the current
works, but
does not. This makes starting a recursion with |
+1 |
How does that help efficiency? |
@satyr I was referring to the byte code size of
vs.
The former may be more efficient in the runtime performance sense; was that the original reasoning behind it? As #1403 reports, it's also causing scoping bugs... (Edit: Ran a quick jsPerf, which does find the |
Yes. See how it came to be: |
That seems like the sort of optimization that makes sense for Coco, but is an odd fit with CoffeeScript, which doesn't reorder code (except to a very limited extent for postfix expressions, of course). It's cool, but it also puts more distance between the CoffeeScript input and the JS output than one expects... |
Maybe. Note that I didn't touch Coffee for this--adopting the patch was Jeremy's choice. |
I think the function should be extracted. It only makes sense to define a single function rather than define a function in each iteration of the loop. |
OK. I'm neutral on the issue of the single function optimization, then. The important thing is to fix the bugs and make |
Howdy. I agree with the proposed additions to |
I agree that |
The reason we call it If CoffeeScript were to support the |
I don't care about Harmony's Also, I don't think people will get confused with the Harmony In short, let's not let other |
What about |
I'm sure eirikurn is being facetious, but on a serious note, what do these three keywords do in other programming languages? |
i also just got bitten by a named |
I think something like this would work well: do (x as y) -> would compile to: (function(y) {
})(x); and do (x = y) -> would compile to: (function(x) {
if (x == null) {
x = y;
}
})(); |
What's the status of this? As of CoffeeScript 1.1.3, do ($ = jQuery) -> still compiles to (function($) {
if ($ == null) $ = jQuery;
})($); as opposed to the clearer and more idiomatic (function($) {
})(jQuery); |
@jimmycuadra: I think you've answered your own question. |
How so? I can't tell if a decision was reached and we're waiting for an implementation, or if more discussion was desired. |
Ah, you were asking about the state of the discussion, not the state of the compiler. I think it is still being discussed, but generally favourable. A good patch would probably be accepted, and at least revive the discussion. |
@TrevorBurnham: The above patch should satisfy your wishes ... Here's the test case: do (nonExistent = 'one') ->
eq nonExistent, 'one'
two = 2
do (one = 1, two, three = 3) ->
eq one, 1
eq two, 2
eq three, 3
do func = (two, func) ->
eq two, 2
eq func, func |
This is very welcome. Thanks, Jeremy. |
Yes, thanks. :) This is a huge improvement IMO. |
Jeremy has just added satyr's implementation of
do
to master in response to issue 959. Thedo
syntax was originally discussed at issue 788.As of the current master,
do (x = y) -> ...
compiles totreating
y
as a default value ofx
;which is really just an inefficient way of writingChanging
do
to rewrite in this way would also allow you to writedo (x = x)
, which currently is nonsensical, to capture a particular value ofx
(useful in loops). I'd furthermore propose thatdo (x)
be made a shorthand fordo (x = x)
, just as{x}
is a great shorthand for{x: x}
.The text was updated successfully, but these errors were encountered: