-
Notifications
You must be signed in to change notification settings - Fork 4
CS2 Discussion: Output: Destructuring #69
Comments
Don't you mean this for destructuring objects with ({a: this.a, b: this.b, c: this.c} = pairs); Note that there is one big difference between CS destructuring and ES. Generators. I can't see any problems with your suggested compilation (other than what I mentioned above). |
@lydell Thanks, you’re correct. Updated. I guess this means that no temporary variables are required? That simplifies things. Not that I don’t want to do this, but it’s not like supported ES destructuring is required. Your example could simply be written as: a = generator()
b = generator() Though one could argue that anything Babel transpiles can be written in ES3 in some form or another. But this edge case is easier (and less boilerplatey) than most. |
You mean this: gen = generator()
a = gen.next().value
b = gen.next().value Anyhow, your suggested output change seems totally correct, and seems to only bring benefits (full ES compatibility for free, nicer code outout), so I can't see why we would not do this. |
It’s not that we wouldn’t do this, it’s more of a question of priority. Like I think we can release CS2-alpha1 without this being ready (but not before jashkenas/coffeescript#4424 I think). Are there any edge cases I’m not thinking of? The MDN page has some interesting things like default values, skipped values, matches via regex, etc. |
|
For class B extends A
constructor: (@name) ->
super Should continue to compile to: class B extends A {
constructor (name) {
super(...arguments)
this.name = name
}
} Otherwise this could potentially clean up a lot of code! |
@connec Do you mean this? class B extends A
constructor: ({@name}) ->
super → class B extends A {
constructor(arg) {
super(...arguments)
({name: this.name} = arg);
}
} |
Whoops, @lydell, I do indeed 😅 |
Skipped values in array destructuring seems like a cool feature. I think ideally that could be added to CS1 and CS2. Perhaps worth forking off into a separate issue, at least for CS1. |
If I need skipped values I usually just assign to a veriable I won't use: [a, _, _, b] = [1, 2, 3, 4] Also there is another edge case from coffeescript that I do not believe ES6 covers? [a, ..., b] = [1, 2, 3, 4] I don't think the [a, ..., b, c, d, e] = generator() If the generator only emits 4 values or infinite values, what does the above snippet mean? CS1 is assuming we are working with an |
@JavascriptIsMagic Agreed, those |
@jashkenas or @lydell am I correct in understanding that the current destructuring implementation is in I have a hunch that this will turn into something similar to updating function parameters, where we can output ES syntax in some cases but need to fall back to the 1.x implementation for cases like expansions that aren’t compatible with ES. So maybe this function can shrink a little with outputting destructuring as ES, but big chunks of complexity will likely remain. |
Yes, that's the place. I added some quick comments to it for you: lydell/coffee-script@590cd3f
Also, it might be worth thinking about if we want to start supporting |
Does this mean you're working on destructuring @GeoffreyBooth? |
I’ve started a branch, but not really done any work yet: https://github.com/GeoffreyBooth/coffeescript/tree/destructuring Feel free to work on this branch if you’d like. I’m not going to hold up CS2-alpha1 for this feature, though. |
I’ve opened a pull request for discussion of that branch: jashkenas/coffeescript#4478 |
Migrated to jashkenas/coffeescript#4962 |
Building off of #18, we should consider changing CoffeeScript’s destructuring output from the current ES3 to ESNext syntax. So this:
which currently compiles to this:
would instead compile to this (?):
The array destructuring, as you can see here, is pretty straightforward; but the object destructuring presents issues. First, if we’re not declaring the variable at the same time (as we never do in CoffeeScript, hence the
var
line of declarations at the top of each scope) then we need to wrap the destructuring assignment in parentheses, per MDN. And if the target variable is attached tothis
, we need to use the “assigning to new variable names” shortcut to destructure into our intended destination variable, unless people have a better suggestion for how to handle this.There’s also the question of whether this is worth the effort. The current output is already rather readable, so the gains aren’t great. Destructuring, whether in CS or ES, is just a time-saver; anything you can do with destructuring you can also already do in more verbose variable assignment, so there’s no compatibility need for us to change anything. If anything, the lack of need behooves us to make sure that if we make this change, it is done in a fully backward compatible way.
Edit: Fixed destructuring object with
this
per @lydell’s correction.The text was updated successfully, but these errors were encountered: