This repository has been archived by the owner on Feb 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
CS2 Discussion: Output: Compiling do to let #61
Labels
Comments
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: |
I think for loops should just use |
Oh well, I thought that we are OK with breaking changes for CoffeeScript 2. |
Closing as we didn’t end up implementing this in 2. |
coffeescriptbot
changed the title
Compiling do to let
CS2 Discussion: Output: Compiling do to let
Feb 19, 2018
This was referenced Feb 19, 2018
Migrated to jashkenas/coffeescript#4954 |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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 #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 #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: