-
Notifications
You must be signed in to change notification settings - Fork 2k
[WIP] Allow foo.'bar-bat' and foo.1 to access object members #3828
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
Conversation
👍 This makes for a nice symmetry with literal object notation. obj =
# Can use barewords for keys that look like identifiers.
foo: 42
# But need enclosing commas for non-identifier-like keys.
'wordy key': 43
# Can access identifier-like keys by a bareword after the dot.
obj.foo # => 42
# But need enclosing commas for accessing non-identifier-like keys.
obj.'wordy key' # => 43 |
I'm not sure really what to think about this. There is a certain symmetry, but there's also a certain awkwardness, especially when it comes to more complicated accesses:
... that's just odd. Any staunch defenders of this idea? |
It works in coco/LS, but I admit I don't use it that much. It's only interesting if you have to deal with "strange" json schemas... |
If this is going to be supported, numbers should be as well. |
@lydell: Agreed. I'm 👍 for both of them. |
@lydell maybe it would be easiest to do something like
and then replacing the existing uses of |
I always try to avoid the |
Alright then. I'm persuaded. Let's add numbers to this patch, and some more robust tests, and merge. |
😀 |
@pscollins Please do what you suggested and add tests for all the access variants (not just |
@lydell |
@pscollins: Of course those should be allowed. a::' ' = ->
a.prototype = ' ': -> |
Yes. If it's for any accessor, it should be for all accessors. And tested ;) |
I've got it working and tested for every form except
It's listed in the testcases I just pushed, commeted out. Uncommenting it yields:
I assume that what's happening is I'm not sure how this could be dealt with in the parser. The best case I can imagine would be adding in a special It's also worth noting that something like
will be ambiguous. What's the best way to approach it? Make a special case for numeric accessors, and forbid non-integers? Require that numeric accessors go in parens? Restrict it to strings? For comparison, it looks like coco basically forbids non-integers:
but that seems really confusing to me. |
Re: the parser error, I don't know — but as to your second question...
... would simply be:
no? I don't see the ambiguity. |
@jashkenas Sorry, I didn't mean that defining the object would be ambiguous -- I meant accessing it would be ambiguous. My worry is that Or you might have
Now, what should It seems to me like it would be easy for someone new to the language to get tripped up by floating point numbers behaving differently from other kinds of numbers, but maybe I'm overreacting? |
It should be a decimal integer, not floats, not hex, not scientific notation, etc. So my answer is it would be |
o '. AccessorIdentifier', -> new Access $2 | ||
o '?. AccessorIdentifier', -> new Access $2, 'soak' | ||
o ':: AccessorIdentifier', -> [LOC(1)(new Access new Literal('prototype')), LOC(2)(new Access $2)] | ||
o '?:: AccessorIdentifierIdentifier', -> [LOC(1)(new Access new Literal('prototype'), 'soak'), LOC(2)(new Access $2)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's correct in the compiled JS though. Remember to rebuild everything.
I think |
You forgot this., |
# identifiers, string literals, and numbers. | ||
AccessorIdentifier: [ | ||
o 'Identifier' | ||
o 'AlphaNumeric' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A possible solution for the number problem could be to replace this line with:
o 'STRING'
o 'INTEGER'
Then, in AlphaNumeric
replace o 'NUMBER'
with o 'Number'
and add:
Number: [
o 'NUMBER'
o 'INTEGER'
]
and adjust the NUMBER
regex in the lexer to match integers separately from other decimal numbers and make Lexer::numberToken
tag integers as such.
I forgot we even allowed leading dots in number literals. Who thought that was a good idea? |
Me too. That’s really making things more complicated here. |
I think the best way to solve it is to add |
It's not a good idea ;) We shouldn't have let that sneak in. |
See #383. |
Agreed. |
That makes it sound like they were never intended but there were no tests for it and during some refactor they were allowed by mistake. Since that's not the case, do you think that we should remove them (which makes this PR a lot simpler to finish, but likely results in 10 duplicate issues the same day it is released) or let them stay? |
Ha ha. There's a certain class of feature (of which there are dozens of examples in CoffeeScript) that I intentionally didn't include at the beginning because they're not my personal preferred style ... but that managed to sneak in over time because people ask for them, and you say "why not?". This was one of those. I don't think we can remove them at this point. But perhaps we could tag them specially in the lexer (or something) to make this change easier. |
Numbers with a leading |
I'd love it if that were really the case. My anecdotal sense is that lots of folks prefer to write Can we employ Github's code search to find out if folks are using it in CoffeeScript? |
At least in CSS I’ve seen lots of |
Just found that this would close #1334 as well. |
Any status on this PR? Seems like the issue was related to a possible breaking change if some people used numbers like this: Is breaking this use case the reason for this PR not being accepted? |
|
@shreeve IIRC I had trouble getting it to work in the |
FYI we should probably put this on hold until tagged template literals are supported, unless we’re certain that there won’t be any ambiguity between this proposed syntax and the syntax we’ll need for that. I would think tagged template literals should have a much higher priority if we’re forced to choose between them. |
@pscollins or anyone on this thread, I’d love to get this updated and targeted against the |
What’s going on with this? Is this something we’re still interested in getting finished, or should I close this as an abandoned effort? If we are trying to finish it, are there any breaking changes that would result from merging this in? |
I can't speak to the quality of this PR, but this is a great feature and should be pursued. |
At this point the codebase has changed so much since this PR was started that a new PR will be necessary, so I’m closing this one. I’m leaving issue #1334 open as a reminder that we’re interested in pursuing this; that issue has a link to this thread. |
This change addresses #3520. I wasn't sure if there is a consensus on that issue, but I think that it's a simple change that could clean up code in some places.