-
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: Destructuring #4962
Comments
From @lydell on 2017-01-23 06:46 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). |
From @GeoffreyBooth on 2017-01-23 06:54 @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. |
From @lydell on 2017-01-23 07:03 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. |
From @GeoffreyBooth on 2017-01-23 07:22 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 #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. |
From @lydell on 2017-01-23 07:51
|
From @connec on 2017-01-23 11:15 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! |
From @lydell on 2017-01-23 11:30 @connec Do you mean this? class B extends A
constructor: ({@name}) ->
super → class B extends A {
constructor(arg) {
super(...arguments)
({name: this.name} = arg);
}
} |
From @connec on 2017-01-23 12:56 Whoops, @lydell, I do indeed 😅 |
From @edemaine on 2017-01-23 13:44 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. |
From @JavascriptIsMagic on 2017-01-24 20:04 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 |
From @edemaine on 2017-01-24 21:39 @JavascriptIsMagic Agreed, those |
From @GeoffreyBooth on 2017-02-05 01:13 @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. |
From @lydell on 2017-02-05 10:40 Yes, that's the place. I added some quick comments to it for you: lydell@590cd3f
Also, it might be worth thinking about if we want to start supporting |
From @connec on 2017-02-06 09:59 Does this mean you're working on destructuring @GeoffreyBooth? |
From @GeoffreyBooth on 2017-02-06 16:09 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. |
From @GeoffreyBooth on 2017-03-29 04:58 I’ve opened a pull request for discussion of that branch: #4478 |
From @GeoffreyBooth on 2017-01-23 03:58
Building off of coffeescript6/discuss#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: