# Confused by macros and their hygene

**URL:** <https://discourse.julialang.org/t/confused-by-macros-and-their-hygene/73362>\
**Category:** General Usage\
**Created:** [December 20, 2021, 9:52am UTC](https://discourse.julialang.org/t/confused-by-macros-and-their-hygene/73362 "2021-12-20T09:52:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JackStrauss](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@JackStrauss](https://discourse.julialang.org/u/JackStrauss)\
**Post date:** [December 20, 2021, 9:52am UTC](https://discourse.julialang.org/t/confused-by-macros-and-their-hygene/73362/1 "2021-12-20T09:52:24Z")

</div>

Hello

I know there is a simple solution to this but my search skills have failed me. Suppose I have a a macro like

```julia
macro P(ex)
    return quote
        if Print
            println($ex)
        end
    end
end

```

then Print=true and @P “text” will work  
@macroexpand @P “text” shows why

but how can I do this inside a function?

```julia
function f()
  Print=true
   @P "text"
end

```

I read all I can find about macro hygene and esc() and still I cannot make it work.

I know there is something incredibly simple that escapes me.

best, Jack

---

<div class="post-metadata">

**Author:** ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)\
**Post date:** [December 20, 2021, 10:06am UTC](https://discourse.julialang.org/t/confused-by-macros-and-their-hygene/73362/2 "2021-12-20T10:06:18Z")

</div>

You’ll see in the macroexpanded expression:

```julia
julia> @macroexpand @P "text"
quote
    #= REPL[1]:3 =#
    if Main.Print
        #= REPL[1]:4 =#
        Main.println("text")
    end
end

```

that `Print` refers to `Main.Print`, which is a global that’s expected to exist in the `Main` module, but in `f` you want it to refer to a local variable called `Print`. You’ll need to `esc` the `Print` in your returned expression from the macro:

```julia
julia> macro P(ex)
           return quote
               if $(esc(:Print))
                   println($ex)
               end
           end
       end
@P (macro with 1 method)

julia> @macroexpand @P "text"
quote
    #= REPL[3]:3 =#
    if Print
        #= REPL[3]:4 =#
        Main.println("text")
    end
end

```

---

<div class="post-metadata">

**Author:** ![JackStrauss](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@JackStrauss](https://discourse.julialang.org/u/JackStrauss)\
**Post date:** [December 20, 2021, 10:13am UTC](https://discourse.julialang.org/t/confused-by-macros-and-their-hygene/73362/3 "2021-12-20T10:13:35Z")

</div>

Mike

Wow, I knew it was something simple, in my case I understood esc, but not :Print vs. Print, I see the difference now. I will redouble my efforts to understand metaprogramming on a deeper level.

This is why Julia is so great, the helpfulness of the community. Thanks Mike.

best Jack

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [December 20, 2021, 11:06am UTC](https://discourse.julialang.org/t/confused-by-macros-and-their-hygene/73362/4 "2021-12-20T11:06:14Z")

</div>

> [@JackStrauss](#):
>
> This is why Julia is so great, the helpfulness of the community. Thanks Mike.

I humbly dispute this assertion.

I enjoyed Julia for years _without_ any contact with the “community” 🙂
