-
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
CS2 Discussion: Output: Compiling do to let #4954
Comments
From @JavascriptIsMagic on 2016-12-13 01:03 What about changing the output of array = [1, 2]
value = 'outside'
do for value in array
console.log 'Inside:', value
console.log 'Outside:', value Which would compile to: var array = [1, 2]
var value = 'outside'
for (let value, index = 0; index < array.length; index++) {
value = array[index]
console.log('Inside:', value)
}
console.log('Outside:', value) It currently compiles but always throws an error, because it tries to call an array as anonymous function like this: |
From @mitar on 2016-12-13 03:04 I think for loops should just use |
From @edemaine on 2016-12-13 03:24 @JavascriptIsMagic That's a neat extension to the proposal. (Similar, of course, to @mitar I fear that would break a lot of existing code, as discussed in coffeescript6/discuss#63, coffeescript6/discuss#58, coffeescript6/discuss#35. |
From @mitar on 2016-12-13 04:01 Oh well, I thought that we are OK with breaking changes for CoffeeScript 2. |
From @GeoffreyBooth on 2017-11-25 06:03 Closing as we didn’t end up implementing this in 2. |
From @edemaine on 2016-12-12 19:09
I propose that CS2 should compile
into
Unless I'm missing something, this should have identical behavior to the current compilation:
The proposed compilation more readable: other than the braces, it should be best-practices ES6. And it should be a lot faster on ES6, using built-in block scopes instead of creating and calling a function just for a scope change: a very simple benchmark on Node suggests it's about 2.5x faster.
Note that this proposal makes
do
act a lot like alet
block (though there is a slight difference:let (x) ...
would be essentiallydo (x = undefined) -> ...
under this proposal).let
blocks were briefly proposed by @kirly-af on coffeescript6/discuss#35. Key quote by @GeoffreyBooth from that thread:But we already have
do
in CS, so I don't think this problem applies here. I feel like this proposal is consistent with the intended meaning/spirit ofdo
, while exploiting ES6 for readability and speed.Special case:
do f
(wheref
is any expression that doesn't match(args) -> ...
) currently compiles tof()
. This behavior would remain unchanged by this proposal.This proposal also reduces the need for
:=
(as proposed in coffeescript6/discuss#58): it enables the creation of variables scoped to explicit blocks using CS's existingdo
functionality/notation.{ x := 5; ... }
would be equivalent todo (x = 5) -> ...
; both would compile to{ let x = 5; ... }
.The text was updated successfully, but these errors were encountered: