jashkenas / coffeescript

Unfancy JavaScript
https://coffeescript.org/
MIT License
16.51k stars 1.99k forks source link

String Interpolation in keys of object literals #1731

Closed STRd6 closed 13 years ago

STRd6 commented 13 years ago

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 commented 13 years ago

Related: #786

michaelficarra commented 13 years ago

closing as duplicate of #786

STRd6 commented 13 years ago

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

michaelficarra commented 13 years ago

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 commented 13 years ago

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

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

jashkenas commented 13 years ago

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 commented 13 years ago

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

525c1e21-bd67-4735-ac99-b4b0e5262290 commented 13 years ago

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

satyr commented 13 years ago

FYI:

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 commented 13 years ago

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

amnjdm commented 11 years ago

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 commented 11 years ago

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