Skip to content
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

Closed
coffeescriptbot opened this issue Feb 19, 2018 · 5 comments
Closed

CS2 Discussion: Output: Compiling do to let #4954

coffeescriptbot opened this issue Feb 19, 2018 · 5 comments

Comments

@coffeescriptbot
Copy link
Collaborator

coffeescriptbot commented Feb 19, 2018

From @edemaine on 2016-12-12 19:09

I propose that CS2 should compile

do (x, y = 5) -> f x, y

into

{ let x2 = x, y = 5; f(x2, y) }

Unless I'm missing something, this should have identical behavior to the current compilation:

(function (x, y) { f(x, y) })(x, 5)

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 a let block (though there is a slight difference: let (x) ... would be essentially do (x = undefined) -> ... under this proposal). let blocks were briefly proposed by @kirly-af on coffeescript6/discuss#35. Key quote by @GeoffreyBooth from that thread:

the let block was a standard that was proposed but then retracted, so I don’t think it’s something we should adopt

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 of do, while exploiting ES6 for readability and speed.

Special case: do f (where f is any expression that doesn't match (args) -> ...) currently compiles to f(). 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 existing do functionality/notation. { x := 5; ... } would be equivalent to do (x = 5) -> ...; both would compile to { let x = 5; ... }.

@coffeescriptbot
Copy link
Collaborator Author

From @JavascriptIsMagic on 2016-12-13 01:03

What about changing the output of do for to output for (let Like described here?

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: ([])()

@coffeescriptbot
Copy link
Collaborator Author

From @mitar on 2016-12-13 03:04

I think for loops should just use let to bind variables in new version of CoffeeScript.

@coffeescriptbot
Copy link
Collaborator Author

coffeescriptbot commented Feb 19, 2018

From @edemaine on 2016-12-13 03:24

@JavascriptIsMagic That's a neat extension to the proposal. (Similar, of course, to for let proposals e.g. from #35, but a little more "Coffeescripty".)

@mitar I fear that would break a lot of existing code, as discussed in coffeescript6/discuss#63, coffeescript6/discuss#58, coffeescript6/discuss#35.

@coffeescriptbot
Copy link
Collaborator Author

From @mitar on 2016-12-13 04:01

Oh well, I thought that we are OK with breaking changes for CoffeeScript 2.

@coffeescriptbot
Copy link
Collaborator Author

From @GeoffreyBooth on 2017-11-25 06:03

Closing as we didn’t end up implementing this in 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant