2
votes

I'm trying to figure out how to have a quote block, when evaluated, return a symbol. See the example below.

function func(symbol::Symbol)
  quote
    z = $symbol
    symbol
  end
end

a = 1
eval(func(:a)) #this returns :symbol. I would like it to return :a
z
1

1 Answers

5
votes

The symbol your function returned where the symbol function, due to the last symbol in your qoute did not have $ in front. The second problem is you would like to return the symbol it self, which requires you make a quote inside the quote similar to this question Julia: How do I create a macro that returns its argument?

function func(s::Symbol)
   quote
        z = $s
        $(Expr(:quote, s))  # This creates an expresion inside the quote
   end
end
a = 1
eval(func(:a)) #this returns :a
z