# How to get the name of macro calling function?

**URL:** <https://discourse.julialang.org/t/how-to-get-the-name-of-macro-calling-function/72286>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 30, 2021, 11:33am UTC](https://discourse.julialang.org/t/how-to-get-the-name-of-macro-calling-function/72286 "2021-11-30T11:33:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [November 30, 2021, 11:33am UTC](https://discourse.julialang.org/t/how-to-get-the-name-of-macro-calling-function/72286/1 "2021-11-30T11:33:21Z")

</div>

Hey al 🙂

It’s probably easier to ask the question with an example:

```julia
module M
macro m()
    get_name_of_calling_function() |> println
    get_name_of_calling_module() |> println
end

function f()
    @m()
    return
end
end

```

```julia
julia> M.f()
f
M

```

Is that possible?

Cheers

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [November 30, 2021, 1:01pm UTC](https://discourse.julialang.org/t/how-to-get-the-name-of-macro-calling-function/72286/2 "2021-11-30T13:01:34Z")

</div>

In general, my advice when writing macros is to forget you’re writing a macro and ask “how do I do X in code?” Then you just generate that code inside your macro.

With that in mind, I think the answers to your question are likely to be something like:

- `StackTraces.stacktrace()[1].func`
- `@ __MODULE__ `

```julia
module M

macro m()
    quote
        println(StackTraces.stacktrace()[1].func)
        println(@ __MODULE__ )
    end
end

function f()
    @m()
    return
end

end

```

```julia
julia> M.f()
f
Main.M

```

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [November 30, 2021, 1:10pm UTC](https://discourse.julialang.org/t/how-to-get-the-name-of-macro-calling-function/72286/3 "2021-11-30T13:10:45Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [November 30, 2021, 1:48pm UTC](https://discourse.julialang.org/t/how-to-get-the-name-of-macro-calling-function/72286/4 "2021-11-30T13:48:56Z")

</div>

Why can I use `@ __MODULE__ ` in the macro but not `@ __FILE__ ` or `@ __LINE__ `. (Instead I have to use ` __source__.file` or ` __source__.line`)
