You can write this:
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> 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,)