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

String Interpolation in keys of object literals #1731

Closed
STRd6 opened this issue Sep 25, 2011 · 12 comments
Closed

String Interpolation in keys of object literals #1731

STRd6 opened this issue Sep 25, 2011 · 12 comments

Comments

@STRd6
Copy link
Contributor

STRd6 commented Sep 25, 2011

The following code:

data = 
  "#{target.attr('name')}": target.attr('value')

Gives the following error:

PARSE ERROR ON LINE NAN: UNEXPECTED '{'

I would expect to be able to interpolate string keys of object literals.

@525c1e21-bd67-4735-ac99-b4b0e5262290

Related: #786

@michaelficarra
Copy link
Collaborator

closing as duplicate of #786

@STRd6
Copy link
Contributor Author

STRd6 commented Sep 26, 2011

What was the reasoning behind the close? To me it seems like a bug, or at least an inconsistency.

@michaelficarra
Copy link
Collaborator

Maybe you should ask @jashkenas in #786. I remember that we decided to remove dynamic keys, but not why. And it wasn't explained/linked in the commit.

@525c1e21-bd67-4735-ac99-b4b0e5262290

I asked ages ago in #786 but he hasn't replied.

I guess this is "just one of those things".

@jashkenas
Copy link
Owner

Sorry for being tardy -- if I remember correctly, it was because some of our other language features depend on having the key known at compile time. For example, method overrides and super calls in executable class bodies. We want to know the name of the key so that a proper super call can be constructed.

Also, it makes it so that you have to closure-wrap objects when used as expressions (the common case) whenever you have a dynamic key.

Finally, there's already a good syntax for dynamic keys in JavaScript which is explicit about what you're doing: obj[key] = value.

There's something nice about having the {key: value, key: value} form be restricted to "pure" identifiers as keys.

@STRd6
Copy link
Contributor Author

STRd6 commented Sep 28, 2011

Thanks for the response, it's well reasoned, and I can support it.

@525c1e21-bd67-4735-ac99-b4b0e5262290

Thank you for clarifying this for us @jashkenas. I've made a note of this in the original ticket.

@satyr
Copy link
Collaborator

satyr commented Sep 28, 2011

FYI:

  • Dynakey is in a ES.next harmony proposal (scroll down to Object Literal Computed Property Keys).
  • If you miss it, try Coco.

For example, method overrides and super calls in executable class bodies. We want to know the name of the key so that a proper super call can be constructed.

True, but orthogonal to this issue.

$ coffee -bpe 'A::x = -> super'
A.prototype.x = function() {
  return A.__super__.x.apply(this, arguments);
};

$ coffee -bpe 'A::[x] = -> super'
SyntaxError: cannot call super on an anonymous function.

it makes it so that you have to closure-wrap objects when used as expressions (the common case) whenever you have a dynamic key

Wrong. You never need to IIFE-wrap them since objects are always expressions.

@michaelficarra
Copy link
Collaborator

@satyr: the dynakey proposal isn't looking good. Good riddance, I didn't like it at all.

@amnjdm
Copy link

amnjdm commented Mar 14, 2013

I know this has been closed and I'm sure no one would like to comment on or answer this but I am going to point something out that relates to coffeescript and store.js:

It would be super helpful to be able to do string interpolation for store.js ...
I can't reference values from a comprehension to add to the key making it impossible to save now.

do_something = for num in [5...1]
store.set('user,{
name#{num}: value#{num})
})

store.js README: https://github.com/marcuswestin/store.js/#readme

@vendethiel
Copy link
Collaborator

@wackfordjf3 Not sure I read correctly your code, but you should re-read the linked ticket

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

7 participants