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

Allow underscored identifiers if within accented quotes #9014

Closed
wants to merge 2 commits into from

Conversation

Vindaar
Copy link
Contributor

@Vindaar Vindaar commented Sep 19, 2018

This PR slightly modifies the grammar for identifier names by allowing arbitrary underscores in identifiers iff they are written in accent quotes `, e.g.

let `__x__` = 5

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:

template genProc(name: untyped): untyped =
  proc `get_ name`[T](x: T): T =
    discard

genProc(number)

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:

import nimpy

let np = pyImport("numpy")
echo np.`__dict__`

cc: @erhlee-bird, @alehander42, @yglukhov

@Araq
Copy link
Member

Araq commented Sep 19, 2018

echo np.`__dict__` is ugly and I don't want more ugliness just because "Python interop" (which will always be subpar anyway, Python is too dynamic). -1 from me.

@alehander92
Copy link
Contributor

I like it,
I guess "" is not ugly, because Nim uses it a lot, so __` is ugly: well that's very subjective. also if the user doesn't have that, he'll just use an uglier alternative, e.g. field("name")

@Araq
Copy link
Member

Araq commented Sep 19, 2018

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".

@alehander92
Copy link
Contributor

alehander92 commented Sep 19, 2018 via email

@Vindaar
Copy link
Contributor Author

Vindaar commented Sep 19, 2018

echo np.`__dict__` is ugly

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.

"Python interop" (which will always be subpar anyway, Python is too dynamic)

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. :)

@yglukhov
Copy link
Member

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.

@dom96
Copy link
Contributor

dom96 commented Sep 19, 2018

+1 from me.

Python interop is a good enough reason to have this.

@@ -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
Copy link
Contributor

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?

Copy link
Contributor Author

@Vindaar Vindaar Sep 19, 2018

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.

Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Copy link
Contributor Author

@Vindaar Vindaar Sep 19, 2018

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.

@Vindaar Vindaar force-pushed the allowQuotedUnderscoreIdents branch from 98ebd56 to 06d264b Compare September 19, 2018 20:17
@Araq
Copy link
Member

Araq commented Sep 28, 2018

I'm pretty sure this wouldn't be good enough, identifier comparisons then also have to consider leading and trailing underscores. Too much complexity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants