# Macro accessing fields of struct at another module

**URL:** <https://discourse.julialang.org/t/macro-accessing-fields-of-struct-at-another-module/87893>\
**Category:** General Usage\
**Tags:** macros\
**Created:** [September 27, 2022, 8:56pm UTC](https://discourse.julialang.org/t/macro-accessing-fields-of-struct-at-another-module/87893 "2022-09-27T20:56:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronneesley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronneesley/32/11072_2.png) [@Ronneesley](https://discourse.julialang.org/u/Ronneesley)\
**Post date:** [September 27, 2022, 8:56pm UTC](https://discourse.julialang.org/t/macro-accessing-fields-of-struct-at-another-module/87893/1 "2022-09-27T20:56:05Z")

</div>

Hello,

I’ve a module with a macro:

```julia
julia> 
module MyModule
  export @mymacro

  macro mymacro(T)
          return fieldnames( eval(T) )
  end
end

```

When I’m at another module or at Julia’s shell I can’t use my macro:

```julia
julia> mutable struct Person
              id::Int
              name::String
       end;

julia> using Main.MyModule

julia> @mymacro(Person)
ERROR: LoadError: UndefVarError: Person not defined
Stacktrace:
 [1] top-level scope
   @ :0
 [2] eval
   @ ./boot.jl:368 [inlined]
 [3] eval(x::Symbol)
   @ Main.MyModule ./REPL[1]:2
 [4] var"@mymacro"( __source__ ::LineNumberNode, __module__ ::Module, T::Any)
   @ Main.MyModule ./REPL[1]:6
in expression starting at REPL[4]:1

```

But if this macro is in the same module, like this:

```julia
julia> macro mymacrosamemodule(T)
              return fieldnames( eval(T) )
        end

julia> @mymacrosamemodule(Person)
(:id, :name)

```

The answer is what I want.

I think the problem is something about Julia [Hygiene](https://docs.julialang.org/en/v1/manual/metaprogramming/#Hygiene), and maybe the function `esc()` could help me, but how to solve this problem?

Thanks

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [September 28, 2022, 2:48pm UTC](https://discourse.julialang.org/t/macro-accessing-fields-of-struct-at-another-module/87893/2 "2022-09-28T14:48:15Z")

</div>

You can write this:

```julia
julia> module MyModule
         export @mymacro
         macro mymacro(T)
           @show T typeof(T) # at macro expansion
           :(fieldnames($(esc(T)))) # returned Expr
         end
       end;

julia> mutable struct Person
          id::Int
          name::String
       end;

julia> using .MyModule

julia> @mymacro Person
T = :Person
typeof(T) = Symbol
(:id, :name)

```

Although the macro isn’t doing anything, really, it may as well just be a function. One problem with `eval` (scope/escaping aside) is that the struct this asks about may not even be defined when the macro runs:

```julia
julia> f(x::T) where T = @mymacro T;
T = :T
typeof(T) = Symbol

julia> f(Person(1, "me"))
(:id, :name)

julia> struct Dog; id::Int; end

julia> f(Dog(2))
(:id,)

```
