Eval/quote symbol

Why doesn’t this work on symbols, and how can I fix it?

function q(x)
    quote y = $x end
end


function f(x)
    eval(q(x))
    y
end

f(5) == 5 # ok
f(:baz) == :baz # error

I am not sure what you want for a “symbol”.

First, eval only works on top level, so only global variables are reachable. f(1) will assign 1 to global variable y.

y = 0
f(1)
println(y) # => 1

If you want eval to work on literal symbols, Meta.quot is needed.

function q(x)
    quote y = $(Meta.quot(x)) end
end