Skip to content

CS2 Discussion: Features: Support splats in object literals (object rest/spread syntax) #4959

@coffeescriptbot

Description

@coffeescriptbot

From @connec on 2016-12-15 18:31

I would love to see object spreads and rests supported via splats in object literals:

{ foo, rest... } = { foo: 1, bar: 2 }
console.log foo, rest # 1 { bar: 2 }
console.log { foo, rest... } # { foo: 1, bar: 2 }

obj = { foo: 1 }
copy = { ...obj }
console.log obj === copy # false

setName = (name, record) -> { name, record... }
console.log setName 'greeting', hello: 'world' # { name: 'greeting', hello: 'world' }

stripName = ({ name, record... }) -> record
console.log stripName name: 'greeting', hello: 'world' # { hello: 'world' }

Compilation in the 2 branch could happily use Object.assign for construction:

// console.log { foo, rest... }
console.log(Object.assign({ foo }, rest))

Destructuring would require more implementation, e.g.

// { foo, rest... } = { foo: 1, bar: 2 }
var ref, ref1, ref2, i, k, foo, rest
ref = { foo: 1, bar: 2 }
ref1 = Object.keys(ref)
ref2 = [ 'foo' ] // compiler-generated list of the non-splat properties
foo = ref.foo
rest = {}
for (i = 0; i < ref1.length; i++) {
  k = ref1[i]
  if (ref2.indexOf(k) < 0) {
    rest[k] = ref[k]
  }
}

We could alternatively wait for the proposal to land in environments, or rely on a transpiler like babel.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions