# Quoting symbols in a local scope

**URL:** <https://discourse.julialang.org/t/quoting-symbols-in-a-local-scope/12060>\
**Category:** General Usage\
**Created:** [June 29, 2018, 12:10pm UTC](https://discourse.julialang.org/t/quoting-symbols-in-a-local-scope/12060 "2018-06-29T12:10:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon32405968](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@anon32405968](https://discourse.julialang.org/u/anon32405968)\
**Post date:** [June 29, 2018, 12:10pm UTC](https://discourse.julialang.org/t/quoting-symbols-in-a-local-scope/12060/1 "2018-06-29T12:10:13Z")

</div>

How do symbols work in a local scope when quoted in a QuoteNode/Meta.quot/Expr(:quote), e.g. a for loop? This is a follow-up to my previous question. I want to add something to the docs that fills in the gaps I discovered through the series of links at the bottom of this question, but I just don’t feel like I understand what’s going on ‘under-the-hood’ anywhere near well enough to do it justice.

Defining a symbol variable and printing it works fine:

```julia
julia> asymbol = :a
:a

julia> @eval ($println)(asymbol)
a

```

But doing the same in a for loop yields an error, I’m guessing because @eval looks for the variable `i` in the global scope :

```julia
julia> for i in (:a,)
       @eval ($println)(i)
       end
ERROR: UndefVarError: i not defined
Stacktrace:
 [1] macro expansion at .\REPL[4]:2 [inlined]
 [2] anonymous at .\<missing>:?

```

Trying to interpolate `i` in the for loop also gives an error because then it looks for `a` in the global scope:

```julia
julia> for i in (:a,)
       @eval ($println)($i)
       end
ERROR: UndefVarError: a not defined
Stacktrace:
 [1] macro expansion at .\REPL[5]:2 [inlined]
 [2] anonymous at .\<missing>:?

```

But using Meta.quot works just fine, how is Meta.quot doing that?

```julia
julia> for i in (:a,)
       @eval ($println)( $(Meta.quot(i)))
       end
a

```

References:

> <https://github.com/JuliaLang/julia/issues/6104>
>
> There now seem to be three ways of generating quoted symbols for the AST. Howeve…r, print\_unquoted doesn't seem to know about Expr(:quote). But on the whole, what is the difference between them, and can we deprecated \`Meta.quot\` and \`Expr(:quote)\`?

> <https://stackoverflow.com/questions/26412515/julia-building-symbol-expressions>

> **[Julia Language Tutorial =\> QuoteNode, Meta.quot, and Expr(:quote)](https://riptutorial.com/julia-lang/example/24364/quotenode--meta-quot--and-expr--quote-)**
>
> Learn Julia Language - QuoteNode, Meta.quot, and Expr(:quote)

> [@Get values of symbols inside a function](https://discourse.julialang.org/t/get-values-of-symbols-inside-a-function/848):
>
> Hi guys! I have one problem: if I have an array of symbols, how can I get their values inside a function scope? Something like this: function f(a::Array{Symbols,1}) a = 1 b = 2 c = 3 # Get the values of the symbols in `a` and return. end f([:a,:b]) Then it should return [1;2]. I know that eval will not work because it operates at global scope. So, is there any way to do this? Thanks, Ronan

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 29, 2018, 1:10pm UTC](https://discourse.julialang.org/t/quoting-symbols-in-a-local-scope/12060/2 "2018-06-29T13:10:35Z")

</div>

These examples are something good to understand but they are not very good for explaining macro usage. The first error you get is due to `eval` evaluating in global scope, not due to the macro. In fact `@eval` have very different evaluation semantics than most other macros so you shouldn’t use it to understand macro’s evaluation rules in general.

Quoting symbols do **NOT** behave different in local scope, the only difference you observe above is due to `eval`. As for interpolation, quoting should generally preserve the indentity of the object, including symbols. They just may not be needed when the object doesn’t have special meaning in the AST. This is why the last one works and the second last doesn’t.
