Get module of a caller


#1
module A
   foo(???) = println("I've been called from $???")
end

module B
    import A
    A.foo()   # should pring "I've been called from B"
end

Is it possible to get this behavior, i.e. find out the caller’s module without requiring it to explicitly pass @__MODULE__? I tired:

foo(; mod=@__MODULE__)

But for obvious reasons it returns the module of foo itself instead of the caller.


#2
julia> module A                       
       function foo()                 
           s = stacktrace()           
           get(s[2].linfo).def.module 
       end                            
       end                            
A                                     
                                      
julia> module B                       
       using A                        
       foo() = A.foo()                
       end                            
B                                     
                                      
julia> B.foo()                        
B                                                                      

Obviously needs error handling, probably only works on 0.6, doesn’t quite do what you want (since it needs to be called in a function defined in B) and is generally bad since it reaches into internals, but that is at least one way to get what you want :slight_smile:


#3

Interesting solution, thanks!

For reference, here’s Julia 0.7 version:

module A                       
function foo()                 
    s = stacktrace()           
    s[2].linfo.def.module 
end                            
end                            

module B                       
using Main.A                        

foo() = A.foo()                

end                            

B.foo()  # ==> Main.B