Current_module() vs @__MODULE__ / They are not equivalent


#1
julia> module XXX
               export foo,bar
               function foo(amod::Module = current_module())
                       println(amod)
               end

               function bar(amod::Module = @__MODULE__)
                       println(amod)
               end
       end
Main.XXX

julia> module YYY
               using Main.XXX
               export f,g
               function f()
                       foo()
               end

               function g()
                       bar()
               end
       end
Main.YYY

julia> using Main.YYY

julia> f()
┌ Warning: `current_module()` is deprecated, use `@__MODULE__` instead.
│   caller = foo at REPL[1]:4 [inlined]
└ @ Core REPL[1]:4
Main

julia> g()
Main.XXX

How can i get the old current_module() functionality?


#2

There’s no equivalent. The exact fix of this depending on what you are actually using this for at a higher level


#3

I am trying to achieve the functionality of foo() which is giving me the module wherever i am calling the function.


#4

That is exactly the thing that is not possible. You need to figure out why you need that behavior.


#5

Or in another word, what does your function do, what does it operate on the module and why does it care about the module the top-level code is running in.