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

compile a.0.1 to a[0][1] #918

Closed
michaelficarra opened this issue Dec 12, 2010 · 15 comments
Closed

compile a.0.1 to a[0][1] #918

michaelficarra opened this issue Dec 12, 2010 · 15 comments

Comments

@michaelficarra
Copy link
Collaborator

Idea from coco, implemented at satyr/coco@c395b035

Anyone against this? It seems like there's no downside, it's convenient, and it looks nicer.

@akidee
Copy link

akidee commented Dec 12, 2010

Good idea

@Sh4rK
Copy link

Sh4rK commented Dec 12, 2010

I agree

@odf
Copy link

odf commented Dec 12, 2010

I think it looks confusing.
coffee> a = { '0.1': 'point one' }
{ '0.1': 'point one' }
coffee> a[0.1]
point one

@TrevorBurnham
Copy link
Collaborator

Yes, +1 from me as well. odf raises a good point about a.0.1 feeling ambiguous if a[0.1] is defined, but keys with .s in them are pretty uncommon. In the much more common case—arr.0, arr.1, etc.—the dot syntax helps to reduce punctuation noise.

@satyr
Copy link
Collaborator

satyr commented Dec 13, 2010

Note that Coco also allows:

a.'0'
a."#{1-1}"

@odf
Copy link

odf commented Dec 13, 2010

As I remarked elsewhere, I would also like to be able to write things like a.+ instead of a["+"]. That would save me three characters each time instead of just one, and get me closer to the conventional notation instead of further away from it. That doesn't necessarily mean I'm against this suggestion, but maybe it would be useful to think a bit more broadly. What can we reasonably put after a dot and expect coffeescript to interpret it as an attribute name?

@TrevorBurnham
Copy link
Collaborator

I'm not sure about a.+, and even a.0.1 makes me a little hesitant. But strings, as in satyr's examples above, seem like a slam dunk to me. Square brackets should be reserved for cases where the key is variable. (Note that a[0] and a['0'] are equivalent; keys are always coerced to strings.) So I think that allowing a.'str'/a."str" would be a pretty conservative, readability-boosting addition to the language.

@michaelficarra
Copy link
Collaborator Author

@TrevorBurnham: While I don't disagree, I'd say that is probably better off as a separate proposal. Let's keep this discussion to the originally-proposed a.2.15 syntax where integers alone are allowed to be used as property names.

@odf
Copy link

odf commented Dec 14, 2010

If it's all about reducing punctuation, wouldn't a[2,15] look more natural and less confusing than a.2.15?

@TrevorBurnham
Copy link
Collaborator

That's actually a really interesting idea, though I'm hesitant to yet another meaning to the once-innocuous string of characters [a, b]...

@satyr
Copy link
Collaborator

satyr commented Dec 15, 2010

Note also that the original commit that implemented this is satyr/coco@b368088c. The one in OP is a bug fix for it.

@jashkenas
Copy link
Owner

I'm afraid I'm rather strongly against this, as it divorces what can be used as dotted accessors from what can be used as a valid variable name. Right now, for any x that you can write obj.x = val, you can also write x = val. After this change, that's no longer the case. Closing as a wontfix.

@satyr
Copy link
Collaborator

satyr commented Dec 18, 2010

for any x

no

@michaelficarra
Copy link
Collaborator Author

satyr's got you on that one

@akidee
Copy link

akidee commented Dec 18, 2010

obj['x'] = val

and

['x'] = val

Alternative:

'x' = val

But how can this be translated into JS? I think the only way is eval()

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

7 participants