Skip to content

Splatted objects #815

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

Closed
satyr opened this issue Oct 27, 2010 · 11 comments
Closed

Splatted objects #815

satyr opened this issue Oct 27, 2010 · 11 comments

Comments

@satyr
Copy link
Collaborator

satyr commented Oct 27, 2010

We already got splatted arrays [a, b...]. Now how about extending it to objects?

coffee> o = un: 42; console.dir {0: 1, o...}
{ '0': 1, un: 42 }

The compilation of {0: 1, o...} would be something like:

__mix({0: 1}, o)

where __mix is defined as:

(target, source) ->
  target[k] = v for k, v of source
  target
@jashkenas
Copy link
Owner

I'm not a big fan of overloading ... even more than it already is. Splats are for lists of things...

@satyr
Copy link
Collaborator Author

satyr commented Oct 27, 2010

This feature would be handy because copying an object's props over another is a very common operation.

Our source code is no different--we even have two different helper fuctions that can be replaced by this.

@sstephenson
Copy link
Contributor

I really like this idea. Almost all of my libraries have some sort of merge helper.

@michaelficarra
Copy link
Collaborator

Like jashkenas, I believe this is too much responsibility for the ... operator. Also, determining and then memorizing the exact semantics of this particular merge would be a nightmare. -1 for now.

@satyr
Copy link
Collaborator Author

satyr commented Oct 27, 2010

Splats are for lists of things...

An object is effectively a list of properties, for that matter.

@TrevorBurnham
Copy link
Collaborator

This doesn't make sense to me. As Jeremy said, this puts a pretty big burden on the splat operator. What's worse,

{x, y} = obj

is valid shorthand for x = obj.x and y = obj.x, but

{a: x, b, c...} = obj

would be...what? Here, a and b are references to keys on obj, and x is a reference to a variable, but what is c? It can't mean "merge obj.c into this object," because there is no object. So is it a syntax error, or is there something useful that it could indicate?

I'd be happy to have a nice syntax for object merges, but this isn't the way to do it.

@satyr
Copy link
Collaborator Author

satyr commented Oct 27, 2010

{a: x, b, c...} = obj

A similar situation to [a..., b...] = arr (multiple splats), where we lack a defined behavior. We'd just throw syntax error in these cases.

@jashkenas
Copy link
Owner

It's not only that -- the extend method really belongs on Object.prototype. It would be a shame if CoffeeScript supported a convenient syntax for mixing an object into an object literal, but lacked a syntax for mixing an object into a preexisting object.

Closing as a duplicate -- this issue has been discussed plenty before.

@satyr
Copy link
Collaborator Author

satyr commented Oct 27, 2010

Yet another issue that's closed too fast, eh?

but lacked a syntax for mixing an object into a preexisting object

That's coming as well, of course.

Closing as a "duplicate" -- this issue has been discussed plenty before

Please provide links. Duplicate of what exactly?

@jashkenas
Copy link
Owner

Sure thing. #218, #377, at least.

@satyr
Copy link
Collaborator Author

satyr commented Oct 31, 2010

This issue was closed.
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

5 participants