# Using eval and a macro with \`$\`

**URL:** <https://discourse.julialang.org/t/using-eval-and-a-macro-with/64570>\
**Category:** General Usage\
**Tags:** macros\
**Created:** [July 13, 2021, 1:37pm UTC](https://discourse.julialang.org/t/using-eval-and-a-macro-with/64570 "2021-07-13T13:37:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 13, 2021, 1:37pm UTC](https://discourse.julialang.org/t/using-eval-and-a-macro-with/64570/1 "2021-07-13T13:37:42Z")

</div>

I’ve been working on a macro which uses `$` in a special way. But for certain tests, I need to use `@eval` or `eval`. Here is a very stupid MWE showin the problem, roughly

```julia
julia> macro with_dollar(x)
           if x isa Expr && x.head == :$
               esc(:(y = 100))
           else
               esc(:(y = 200))
           end
       end;

julia> @with_dollar $a
100

julia> @eval (@with_dollar $a)
ERROR: UndefVarError: a not defined

```

Is there a way to tell `@eval` to ignore it’s own rules about `$`? How do other packages handle this?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 13, 2021, 1:44pm UTC](https://discourse.julialang.org/t/using-eval-and-a-macro-with/64570/2 "2021-07-13T13:44:04Z")

</div>

Can’t you use `eval` instead of `@eval`?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 13, 2021, 1:47pm UTC](https://discourse.julialang.org/t/using-eval-and-a-macro-with/64570/3 "2021-07-13T13:47:55Z")

</div>

Yes, but it gives the same problem

```julia
julia> e = quote 
           @with_dollar $a
       end
ERROR: UndefVarError: a not defined

```

I guess the solution is

```julia
julia> e = quote 
           @with_dollar $(Expr(:$, :a))
       end
quote
    #= REPL[8]:2 =#
    #= REPL[8]:2 =# @with_dollar $(Expr(:$, :a))
end

julia> eval(e)
100

```

Does that seem like the easiest option?
