Skip to content

Commit

Permalink
var"str" string macro for unparsable identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
c42f committed Jul 3, 2019
1 parent f6049d6 commit 4b1e9e6
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ New library functions
* `findfirst`, `findlast`, `findnext` and `findprev` now accept a character as first argument
to search for that character in a string passed as the second argument ([#31664]).
* New `findall(pattern, string)` method where `pattern` is a string or regex ([#31834]).
* The `@var_str` string macro can create variable names which won't parse as normal julia syntax ([#32408]).

Standard library changes
------------------------
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,7 @@ export
@gensym,
@eval,
@deprecate,
@var_str,

# performance annotations
@boundscheck,
Expand Down
21 changes: 21 additions & 0 deletions base/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,24 @@ macro generated(f)
error("invalid syntax; @generated must be used with a function definition")
end
end

"""
@var_str(str) -> Symbol
Refer to a variable named `Symbol(str)`, even when `str` is not a valid name
in normal Julia source code.
This can help interoperability with programming languages which have different
rules for the construction of valid identifiers. For example, to refer to the
`R` variable `draw.segments`, you can use `var"draw.segments"` in your Julia
code.
`@var_str` is also used to `show` julia source code which has been macro
expanded or otherwise contains variable names which can't be parsed normally.
!!! compat "Julia 1.3"
This macro requires at least Julia 1.3.
"""
macro var_str(str)
esc(Symbol(str))
end
1 change: 1 addition & 0 deletions doc/src/base/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ Base.@nospecialize
Base.@specialize
Base.gensym
Base.@gensym
Base.@var_str
Base.@goto
Base.@label
Base.@simd
Expand Down
2 changes: 1 addition & 1 deletion test/choosetests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function choosetests(choices = [])
"checked", "bitset", "floatfuncs", "precompile",
"boundscheck", "error", "ambiguous", "cartesian", "osutils",
"channels", "iostream", "secretbuffer", "specificity",
"reinterpretarray", "syntax", "logging", "missing", "asyncmap"
"reinterpretarray", "syntax", "logging", "missing", "asyncmap", "expr"
]

tests = []
Expand Down
5 changes: 5 additions & 0 deletions test/expr.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@testset "var_str" begin
@test macroexpand(@__MODULE__, :(var"@foo")) == Symbol("@foo")
@test macroexpand(@__MODULE__, :(var"##")) == Symbol("##")
@test macroexpand(@__MODULE__, :(var"a-b")) == Symbol("a-b")
end

0 comments on commit 4b1e9e6

Please sign in to comment.