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

Comments

Projects
None yet
7 participants
@STRd6
Copy link

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.

@pyrotechnick

This comment has been minimized.

Copy link

pyrotechnick commented Sep 25, 2011

Related: #786

@michaelficarra

This comment has been minimized.

Copy link
Collaborator

michaelficarra commented Sep 26, 2011

closing as duplicate of #786

@STRd6

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Collaborator

michaelficarra commented Sep 26, 2011

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.

@pyrotechnick

This comment has been minimized.

Copy link

pyrotechnick commented Sep 27, 2011

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

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

@jashkenas

This comment has been minimized.

Copy link
Owner

jashkenas commented Sep 27, 2011

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

This comment has been minimized.

Copy link

STRd6 commented Sep 28, 2011

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

@pyrotechnick

This comment has been minimized.

Copy link

pyrotechnick commented Sep 28, 2011

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

@satyr

This comment has been minimized.

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

This comment has been minimized.

Copy link
Collaborator

michaelficarra commented Sep 28, 2011

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

@amnjdm

This comment has been minimized.

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

This comment has been minimized.

Copy link
Collaborator

vendethiel commented Mar 14, 2013

@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