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

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

module A
  macro currentnestedlevel()
    # TODO how to do this?
  function f()
    function g()
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.

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.


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

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