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

Using reserved words as NamedTuple field names #26819

Open
GunnarFarneback opened this issue Apr 16, 2018 · 4 comments
Open

Using reserved words as NamedTuple field names #26819

GunnarFarneback opened this issue Apr 16, 2018 · 4 comments
Labels
display and printing Aesthetics and correctness of printed representations of objects.

Comments

@GunnarFarneback
Copy link
Contributor

Followup to #26507.

This works:

julia> a = NamedTuple{(:function,)}((1,))
(function = 1,)

julia> a.function
1

However, direct construction does not work.

julia> a = (function = 1,)
ERROR: syntax: unexpected "="

There are also (at least) two different failure modes for different reserved words.

julia> a = (end = 1,)
ERROR: syntax: unexpected "end"

Would it be mad to allow this? Alternatively, could it give a uniform and more specific error about bad use of reserved word in named tuple?

@JeffBezanson
Copy link
Sponsor Member

In a language with elaborate (non-s-expression) syntax it is unavoidable that some surface syntax forms won't be able to express everything the underlying object model can represent. And I don't think we can or should allow parsing function = 1.

One thing we could do is fix the printing of NamedTuple{(:function,)}((1,)) to use that syntax rather than (function = 1,).

@JeffBezanson JeffBezanson added the display and printing Aesthetics and correctness of printed representations of objects. label Apr 16, 2018
@quinnj
Copy link
Member

quinnj commented Aug 7, 2019

I wonder if some of @c42f's work on expression parsing and such would facilitate the requested change in printing here (i.e. printing the long-form NamedTuple instead of (function = 1,))

@c42f
Copy link
Member

c42f commented Aug 7, 2019

@quinnj I assume you refer to #32408? With the current syntax from that PR I think we could have (var"function" = 1,) in principle and that would be quite a general way of distinguishing between identifiers and keywords.

I think this would require additional changes to the parser in addition to #32408 and the pretty printer would definitely need more work. But it's a neat connection.

@c42f
Copy link
Member

c42f commented Sep 25, 2019

With #32408 merged we now have

julia> a = (var"function" = 1,)
(function = 1,)

So one part of this is solved.

The second part would be to detect reserved words when showing Exprs and print them with the var syntax so that this can be parsed again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
display and printing Aesthetics and correctness of printed representations of objects.
Projects
None yet
Development

No branches or pull requests

4 participants