# How to get to know in which subfunction a macro is called?

**URL:** <https://discourse.julialang.org/t/how-to-get-to-know-in-which-subfunction-a-macro-is-called/28877>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 18, 2019, 5:42am UTC](https://discourse.julialang.org/t/how-to-get-to-know-in-which-subfunction-a-macro-is-called/28877 "2019-09-18T05:42:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [September 18, 2019, 5:42am UTC](https://discourse.julialang.org/t/how-to-get-to-know-in-which-subfunction-a-macro-is-called/28877/1 "2019-09-18T05:42:19Z")

</div>

Hi there,

using macros we have access to the special variables ` __modules__ ` which gives me the current module, and ` __source__ ` which gives linenumber and file.

Neither of them can tell me in which subfunction the code is executed.

Lets do a pseudo example

```julia
module A
  macro currentnestedlevel()
    # TODO how to do this?
  end
  function f()
    function g()
      @currentnestedlevel
    end
    g()
  end
end
A.f() # 'A.f.g' or similar

```

We could fake this with a module-level macro which parses this information and puts it into the macro as an extra argument. However this only works with named functions and fails for anonymous ones as we cannot see their unique identifier from the source code alone.

**Why I need this:** From time to time I want to store and access a state for my macro call, so that the second call can be different than the first one, depending on the state. Storing is easy, however accessing seems impossible in local scope, so I have to store the information at module level and hence cannot distinguish between macros in nested levels.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 18, 2019, 6:09am UTC](https://discourse.julialang.org/t/how-to-get-to-know-in-which-subfunction-a-macro-is-called/28877/2 "2019-09-18T06:09:37Z")

</div>

> [@schlichtanders](#):
>
> From time to time I want to store and access a state for my macro call, so that the second call can be different than the first one, depending on the state.

I am not sure this is a good idea. In principle the compiler is free to evaluate macros out of order, or multiple times, etc. Macros should be stateless.

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [September 18, 2019, 10:22am UTC](https://discourse.julialang.org/t/how-to-get-to-know-in-which-subfunction-a-macro-is-called/28877/3 "2019-09-18T10:22:13Z")

</div>

That is actually fine for my use case. Would take a bit longer to explain the full stack.  
Just this: reexecuting the macro should give the same result,  
It just depends on how the macro was called before (i.e. I have a macro with arguments) and this needs to be capjtered in the state

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 18, 2019, 10:31am UTC](https://discourse.julialang.org/t/how-to-get-to-know-in-which-subfunction-a-macro-is-called/28877/4 "2019-09-18T10:31:14Z")

</div>

> [@schlichtanders](#):
>
> **Why I need this:** From time to time I want to store and access a state for my macro call, so that the second call can be different than the first one, depending on the state. Storing is easy, however accessing seems impossible in local scope

Could you explain this a bit more. Does `Base.@locals` help in any way?
