A way to get the current method?

#1

For reflection purposes, I’m curious if there’s a way to get the current method? Something like:

function f(x)
    get_current_method() # would return the f(x) Method
end


function f(x,y)
    get_current_method() # would return the f(x,y) Method
end

Honestly it seems like a long-shot this would exist, but figured I’d ask just in case, or maybe I’ll learn something from someone explaining why this can never exist in Julia :slight_smile:

#2

Depending on what you want to do with the result, you might be able to get what you need from the built-in stacktrace() function.

1 Like
#3

Nice, thanks, glad I asked!

julia> get_current_method() = stacktrace()[2].linfo.def
get_current_method (generic function with 1 method)

julia> function f(x)
           get_current_method()
       end
f (generic function with 1 method)

julia> function f(x,y)
           get_current_method()
       end
f (generic function with 2 methods)

julia> f(1)
f(x) in Main at REPL[2]:2

julia> f(1,2)
f(x, y) in Main at REPL[3]:2
#4

Mostly for debugging only since we don’t have much else you can do on it now.

3 Likes