-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Allow underscored identifiers if within accented quotes #9014
Conversation
|
I like it, |
It's objectively ugly because underscores are word separators and sep-sep-stuff-youre-actuallyinterestedin-sep-sep makes no sense at the first glance and and on the second glance you can guess "oh, ok, it means it's 'special' in some way". |
But my point is api-s with underscores exist and if those underscores would
end up in the nim code anyway but wrapped in some kind of call
…On Wed, 19 Sep 2018, 13:44 Andreas Rumpf, ***@***.***> wrote:
It's objectively ugly because underscores are word separators and
sep-sep-stuff-youre-actuallyinterestedin-sep-sep makes no sense at the
first glance and and on the second glance you can guess "oh, ok, it means
it's 'special' in some way".
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#9014 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAys-658dQjyoc8NoRttjCaCmWxEtHisks5uciABgaJpZM4WvxvW>
.
|
I totally agree, I don't like it either. But as @alehander42 already said, these things are out there. I can't change e.g. Python. Personally I'd only use it for such use cases. And given that one needs to use backticks, I suppose it's quite unlikely that anyone would willingly use such an API for Nim itself.
May very well be true, but some simple features helping in that regard don't hurt imo (although changing Nim's spec just for that is maybe too much). But either way, if this is not merged, I won't be too sad either. :) |
I was also surprised that such identifiers do not work in backticks, I thought backticks were specifically designed to support all sorts of ugly identifiers. |
+1 from me. Python interop is a good enough reason to have this. |
compiler/lexer.nim
Outdated
@@ -139,6 +139,9 @@ type | |||
# needs so much look-ahead | |||
currLineIndent*: int | |||
strongSpaces*, allowTabs*: bool | |||
inAccent*: int # if this is 1, we're inside backticks. Used to |
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.
Why is this an int and not a bool
?
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.
Uhm, because when I hacked it together originally a few weeks ago, I wanted to toggle it with xor
to avoid
L.inAccent = if L.inAccent: false else: true
.
I agree that it should be a bool
though. Is there a builtin to toggle a bool or is this the most succinct?
edit: Oh, stupid me. I can do the same thing with a bool, haha.
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.
You could write L.inAccent = not L.inAccent
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.
Fixed.
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.
@Stromberg90 A brain is a funny thing. Me writing not L.inAccent
a few lines below, but not thinking about it to flip on assignment. Sigh.
Thanks.
98ebd56
to
06d264b
Compare
I'm pretty sure this wouldn't be good enough, identifier comparisons then also have to consider leading and trailing underscores. Too much complexity. |
This PR slightly modifies the grammar for identifier names by allowing arbitrary underscores in identifiers iff they are written in accent quotes
`
, e.g.is now allowed. While this example surely doesn't justify such a change to the spec, the ones shown below might.
If there's any chance this PR is considered to be merged, I'll happily modify the spec and provide a few test cases. To be fair I'm not sure if this change might have some unforeseen consequences. All tests seem to pass on my machine at least.
Identifier construction
Raised by @rayman22201 here https://gitter.im/nim-lang/Nim?at=5b85e53f58a3797aa3ce3c9d, identifier construction using accented quotes in templates allows for native snake_case:
Sure, underscores are ignored so in practice it's a non issue.
Python interoperability
If Nim wants to attract Python users with really good interoperability, this would be great to have. Python's "dunder" methods use double underscore identifiers, e.g.
__dict__
.Discussed here yglukhov/nimpy#43 using nimpy calls to dunder methods need to be worked around using
callMethod
explicitly currently.With this PR we would be able to just do the following:
cc: @erhlee-bird, @alehander42, @yglukhov