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

ES: Allow deconstructing objects with splats #3894

Closed
flying-sheep opened this issue Mar 10, 2015 · 34 comments
Closed

ES: Allow deconstructing objects with splats #3894

flying-sheep opened this issue Mar 10, 2015 · 34 comments

Comments

@flying-sheep
Copy link

…for parity with ECMA7. currently we can’t do this:

x =
  a: 1
  b: 2
  c: 3

{ a, b, y... } = x
# or
{ a, b, ...y } = x

console.log a, b, y  # → 1 2 {c:3}
@vendethiel
Copy link
Collaborator

Seems like #815 proposed this feature but was closed for other reasons.

@lydell
Copy link
Collaborator

lydell commented Mar 11, 2015

Is that really part of ES6? I've never seen it before.

@flying-sheep
Copy link
Author

ah, you are right, it’s actually part of ES7, included in JSX. i assumed everything of JSX that isn’t <ReactElement/> syntax is a subset of ES6.

here is the implementation

/edit: babel supports it as well (demo)

@lydell
Copy link
Collaborator

lydell commented Mar 12, 2015

Cool! Thanks for those links!

@flying-sheep
Copy link
Author

no problem.

at closing of that other issue: luckily, that’s about something different (i.e. extending objects, not destructuring them)

and @jashkenas’ argument…

Splats are for lists of things

…is also not valid anymore now that ECMAscript itself uses splats for objects.

so this should be good to go!

@flying-sheep
Copy link
Author

now it’s in the compat-table

@bbtfr
Copy link

bbtfr commented Jun 3, 2015

+1 for this, it would be very helpful to write React Apps in CoffeeScript with this feature.

@reduxdj
Copy link

reduxdj commented Jul 7, 2015

Hoping this is added to Coffeescript, is it going to be?

@wmertens
Copy link

So I'd also love to see {b:5, bar..., c} as the shorthand for merge({b: 5}, bar, {c: c}) and while we're at it {[foo]: bar} for _t={},_t[foo]=bar,_t, all very handy things slated for ES6/7.

Should these go into separate issues?

@lydell
Copy link
Collaborator

lydell commented Aug 16, 2015

{[foo]: bar} is already possible: {"#{foo}": bar}

@wmertens
Copy link

Ooh, I didn't know about that, nicer syntax too! That just leaves the spread and rest operations...

@michaelficarra
Copy link
Collaborator

@lydell with the caveat that it doesn't work for symbols.

@vendethiel
Copy link
Collaborator

Well, since @jashkenas said he considered symbols a misfeature, I guess that's fine for coffee?

@michaelficarra
Copy link
Collaborator

Guess so, though I don't agree we can ignore it since standard lib APIs and even syntax depend on values with symbols as keys.

@flying-sheep
Copy link
Author

yeah, we need a way to express this ES2015 in CS:

const iterable = {
    *[Symbol.iterator]() {
        yield 1
        yield 2
    }
}

@wmertens
Copy link

I'd just like to point out that I'm currently mostly writing JSX and that's mostly because of being able to write foo={...props, thing: 5}.

@onemanstartup
Copy link

@wmertens https://github.com/jsdf/coffee-react cofee-react supports spread attributes.

@wmertens
Copy link

wmertens commented Oct 4, 2015

@onemanstartup yeah but I don't want to write xml.
I simply set E to React.createElement and then write things like E Menubar, collapse: false, (E Item label: t.name, t.val for t in items).
Just calling functions is very nice in CS, not so much is JS.

@Jamesernator
Copy link

yeah, we need a way to express this ES2015 in CS:

const iterable = {
*Symbol.iterator {
yield 1
yield 2
}
}

This is something that should probably be an issue of its own as currently the only ways to do this are:

Not inheritable

class SomeIterable
    constructor: (...) ->
        ...
        @[Symbol.iterator] ->
            ...

Sort've defeats the original point of CoffeeScript classes:

class SomeIterable
    constructor: (...) ->

SomeIterable::[Symbol.iterator] = ->
    ...

@lydell
Copy link
Collaborator

lydell commented Nov 20, 2015

@Jamesernator This is an alternative:

class SomeIterable
    constructor: (...) ->

    @::[Symbol.iterator] = ->
        ...

@pvorona
Copy link

pvorona commented Jan 5, 2016

Speaking of cjsx, one of the main feature of object splats (or rest parameters) in JSX is ability to do so:

const {propIamGonaConsume, ...others} = props
return <SomeComponent {...others}/>

As it seems to me, for now it is impossible to do in cjsx, so it would be really nice to resolve this feature request.

@pvorona
Copy link

pvorona commented Jan 23, 2016

Also, it's first time i found that JS can do something coffee can't

@Tsher
Copy link

Tsher commented Mar 18, 2016

Just use ES6 emit instead. may be coffeescript is good support to ES6 ,not ES7

obj = { x: 1, y: 2}
obj1 = {...obj, z: 3}

# ES6 emit
obj = {x: 1, y: 2}
obj1= Object.assign({}, obj, { z: 3 })

@dbackeus
Copy link

dbackeus commented Apr 8, 2016

As immutable object creation in redux etc is becoming all the rage replicating the object splat in coffeescript seems quite relevant.

It would certainly be a nicer solution than using Object.assign, which is also polyfill dependent.

@gucki
Copy link

gucki commented Sep 28, 2016

@jashkenas Would a PR get merged?

@vendethiel
Copy link
Collaborator

It'd certainly start a discussion :-).

@beeplin
Copy link

beeplin commented Oct 4, 2016

+1.

obj = { x: 1, y: 2}
obj1 = {...obj, z: 3}

nowadays this grammar is quite vital when writing react/redux and vue/vuex. hope coffee will be able to do this soon. :)

@benjie
Copy link

benjie commented Oct 5, 2016

I'd be fine with this being simply passed through so Babel can interpret it, like async/await

@GeoffreyBooth
Copy link
Collaborator

This should be supported as part of CoffeeScript 2, which will output ES2015 spread syntax. After #4311 implements the rest operator, spread in objects should probably be the next target. Help wanted.

@beeplin
Copy link

beeplin commented Oct 6, 2016

@GeoffreyBooth, glad to see plans on CoffeeScript 2 ~ Some es6 features do become more and more vital today: import and export will be the key point of tree shaking in rollup and webpack2; some frameworks like react and vue are also designing their syntax to make max use of es6 feathers like object spread. We all like coffee and hope the new version of coffee can be going ahead of es6 again. :)

@GeoffreyBooth
Copy link
Collaborator

import and export are already here, in CS 1.11.

@beeplin
Copy link

beeplin commented Oct 6, 2016

wow, didn't notice that. cool!

@GeoffreyBooth
Copy link
Collaborator

Help wanted! I’ve started a destructuring branch for CoffeeScript 2 to output ES2015+ syntax for destructuring. I’m happy to invite contributors to this branch.

@GeoffreyBooth GeoffreyBooth changed the title Allow deconstructing objects with splats ES: Allow deconstructing objects with splats May 6, 2017
@GeoffreyBooth
Copy link
Collaborator

Implemented via #4493.

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

No branches or pull requests