Skip to content
This repository has been archived by the owner on Feb 19, 2018. It is now read-only.

CS2 Discussion: Output: Compiling do to let #61

Closed
edemaine opened this issue Dec 12, 2016 · 6 comments
Closed

CS2 Discussion: Output: Compiling do to let #61

edemaine opened this issue Dec 12, 2016 · 6 comments

Comments

@edemaine
Copy link

edemaine commented Dec 12, 2016

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 #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 #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; ... }.

@JavascriptIsMagic
Copy link

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

@mitar
Copy link

mitar commented Dec 13, 2016

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

@edemaine
Copy link
Author

@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 #63, #58, #35.

@mitar
Copy link

mitar commented Dec 13, 2016

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

@GeoffreyBooth
Copy link
Collaborator

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

@coffeescriptbot coffeescriptbot changed the title Compiling do to let CS2 Discussion: Output: Compiling do to let Feb 19, 2018
@coffeescriptbot
Copy link
Collaborator

Migrated to jashkenas/coffeescript#4954

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

No branches or pull requests

5 participants