# Macro behavior depends on method of code loading?

**URL:** <https://discourse.julialang.org/t/macro-behavior-depends-on-method-of-code-loading/55483>\
**Category:** New to Julia\
**Tags:** macros\
**Created:** [February 17, 2021, 7:06pm UTC](https://discourse.julialang.org/t/macro-behavior-depends-on-method-of-code-loading/55483 "2021-02-17T19:06:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marty0801](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marty0801/32/12715_2.png) [@marty0801](https://discourse.julialang.org/u/marty0801)\
**Post date:** [February 17, 2021, 7:06pm UTC](https://discourse.julialang.org/t/macro-behavior-depends-on-method-of-code-loading/55483/1 "2021-02-17T19:06:59Z")

</div>

I have two identical versions of a macro that prints all exported names of a given module. One version is imported from a package; the other is defined globally. The former fails while the latter succeeds. What am I doing wrong?

The package:

```julia
module JuliaUtils

macro modulenames_from_package(module_) 
    return quote 
        names($module_)
    end 
end 

end 

```

The REPL:

```julia
using JuliaUtils: @modulenames_from_package
using Example 

macro modulenames_global(module_) 
    return quote 
        names($module_)
    end 
end 

julia> @modulenames_global Example # works 
3-element Array{Symbol,1}:
 :Example
 :domath
 :hello

julia> @modulenames_from_package Example # FAILS -- why???
ERROR: UndefVarError: Example not defined
Stacktrace:
 [1] top-level scope at /home/elliot/proj/cluster/JuliaUtils/src/JuliaUtils.jl:11
```

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [February 17, 2021, 7:10pm UTC](https://discourse.julialang.org/t/macro-behavior-depends-on-method-of-code-loading/55483/2 "2021-02-17T19:10:17Z")

</div>

I am pretty sure this is because of [macro hygine](https://docs.julialang.org/en/v1/manual/metaprogramming/#Hygiene)  
and that you need

```julia
macro modulenames_global(module_) 
    return quote 
        names($(esc(module_))
    end 
end 

```

and that that will work the same no matter if it is invoked from the module it was defined in, vs being invoked from another module.

I don’t have a good way of explaining why this is the case though.

---

<div class="post-metadata">

**Author:** ![marty0801](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marty0801/32/12715_2.png) [@marty0801](https://discourse.julialang.org/u/marty0801)\
**Post date:** [February 17, 2021, 7:12pm UTC](https://discourse.julialang.org/t/macro-behavior-depends-on-method-of-code-loading/55483/3 "2021-02-17T19:12:52Z")

</div>

Thanks, and congratulations on the speediest response ever!
