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

repr on expressions containing $ or a keyword as identifier is not re-evaluable #57076

Open
lucaferranti opened this issue Jan 17, 2025 · 1 comment
Labels
display and printing Aesthetics and correctness of printed representations of objects.

Comments

@lucaferranti
Copy link
Contributor

lucaferranti commented Jan 17, 2025

The title wording is maybe a bit clumsy, but the following example should convey the meaning. calling repr on the expression adds an extra backslash, which leads to a different expression when parsed back.

julia> s = gensym(:$)
Symbol("##\$#1002508")

julia> ex = :($s = 1 + 1)
:(var"##\$#1002508" = 1 + 1)

julia> new_ex = eval(Meta.parse(repr(ex)))
:(var"##\\\$#1002508" = 1 + 1)

julia> ex == new_ex
false
@lucaferranti
Copy link
Contributor Author

More generically, if my expression contains an identifier like var"some_keyword" or var"$" calling repr and parsing back leads to a different expression or error

julia> ex = :(var"$"())
:($())

julia> eval(Meta.parse(repr(ex)))
()

julia> ex = :(var"$$"())
:(var"\$\$"())

julia> eval(Meta.parse(repr(ex)))
:(var"\\\$\\\$"())

julia> ex = :(var"try"())
:(try())

julia> eval(Meta.parse(repr(ex)))
ERROR: ParseError:
# Error @ none:1:3
:(try())
# └───┘ ── try without catch or finally
Stacktrace:
 [1] #parse#3
   @ ./meta.jl:244 [inlined]
 [2] parse
   @ ./meta.jl:236 [inlined]
 [3] parse(str::String; filename::String, raise::Bool, depwarn::Bool)
   @ Base.Meta ./meta.jl:278
 [4] parse(str::String)
   @ Base.Meta ./meta.jl:276
 [5] top-level scope
   @ REPL[124]:1
 [6] top-level scope
   @ none:1

@lucaferranti lucaferranti changed the title repr on expressions containing $ as part of the identifier is not re-evaluable repr on expressions containing $ or a keyword as identifier is not re-evaluable Jan 17, 2025
@nsajko nsajko added the display and printing Aesthetics and correctness of printed representations of objects. label Jan 17, 2025
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

2 participants