How to interpolate a character as a character in a Julia expression?

I would like to create a quote that gets the character :abcstored in a variable xand inserting it into an array a. However, I could only get the variable abc. The syntax :$xseems to be wrong (not what I wanted). What is the syntax for this ?:

julia> x = :abc
julia> expr = quote
         a = []
         push!(a, $x)
         push!(a, :($x))
         push!(a, :$x)
         a
       end
quote  
    a = [] 
    push!(a, abc) 
    push!(a, $(Expr(:quote, :($(Expr(:$, :x))))))
    push!(a, :$ * x) 
    a
end

Required Result:

quote  
    a = [] 
    push!(a, :abc) 
    a
end
+4
source share
1 answer

You need to add another level of quotation using $(Meta.quot(:abc))which is equivalent $(Expr(:quote, :abc)).

Using:

              _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.2 (2017-12-13 18:08 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

Setup:

julia> x = :abc
:abc

julia> es = [x, :x, :(:x), Expr(:quote, x), Meta.quot(x)]    # :(x) is parsed as :x
5-element Array{Any,1}:                                  
 :abc                                                    
 :x                                                      
 :(:x)                                                   
 :(:abc)                                                 
 :(:abc)                                                 

julia> blk = Expr(:block)
quote
end

Click:

julia> push!(blk.args, :(a = []))
1-element Array{Any,1}:
 :(a = [])

julia> blk
quote
    a = []
end

julia> for e in es
           push!(blk.args, :(push!(a, $e)))
       end

julia> blk
quote
    a = []
    push!(a, abc)
    push!(a, x)
    push!(a, :x)
    push!(a, :abc)
    push!(a, :abc)
end

julia> push!(blk.args, :a)
7-element Array{Any,1}:
 :(a = [])
 :(push!(a, abc))
 :(push!(a, x))
 :(push!(a, :x))
 :(push!(a, :abc))
 :(push!(a, :abc))
 :a

Eval:

julia> blk
quote
    a = []
    push!(a, abc)
    push!(a, x)
    push!(a, :x)
    push!(a, :abc)
    push!(a, :abc)
    a
end

julia> eval(ans)
ERROR: UndefVarError: abc not defined
Stacktrace:
 [1] eval(::Module, ::Any) at ./boot.jl:235
 [2] eval(::Any) at ./boot.jl:234

Fix:

julia> deleteat!(blk.args, 2)
6-element Array{Any,1}:
 :(a = [])
 :(push!(a, x))
 :(push!(a, :x))
 :(push!(a, :abc))
 :(push!(a, :abc))
 :a

julia> blk
quote
    a = []
    push!(a, x)
    push!(a, :x)
    push!(a, :abc)
    push!(a, :abc)
    a
end

julia> eval(ans)
4-element Array{Any,1}:
 :abc
 :x
 :abc
 :abc

Finally:

julia> using Base.Meta: quot

julia> x = :abc
:abc

julia> expr = quote
           a = []
           push!(a, $(quot(x)))
           a
       end
quote
    #= REPL[16]:2 =#
    a = []
    #= REPL[16]:3 =#
    push!(a, :abc)
    #= REPL[16]:4 =#
    a
end

julia> eval(ans)
1-element Array{Any,1}:
 :abc
+4
source

Source: https://habr.com/ru/post/1692241/


All Articles