Get function object from method

Given a Method object, how do I get the function it implements? eval(method.module, works, but it doesn’t look all that reliable.

The first type in sig

julia> m = @which sqrt(1.0)
sqrt(x::Union{Float32, Float64}) in Base.Math at math.jl:454

julia> typeof(m)

julia> m.sig
Tuple{typeof(sqrt),Union{Float32, Float64}}

Note that in general you can only get the type of the function and not the function itself since it can be a closure. If it’s not a closure than the function type should be a singleton type and you can get it’s instance.


One possible way to get the function itself:

m = @which sqrt(1.0)
m.sig.parameters[1].instance(9)  # returns 3.0

This is not a public interface. Set your expectations accordingly.

#with closure [CORRECTION. The following is not a closure. see below]
a = 3
m = @which (x->x+a)(0)
m.sig.parameters[1].instance(9)  # returns 12

No it can’t possibly work for closures. Your x -> x + a is NOT a closure.

You are correct! My mistake.

From private message:

    state = 0
    global counter
    counter(x) = state = state + x + 1

Is probably a closure in some sense but it’s not really what closure means in this context. This is a normal function that has a local state spliced in. The main difference is that you can’t create multiple independent instances of counters. Also note that I believe this syntax is deprecated on master and will require explicit splicing.

1 Like

Thanks a lot, that code has been working great for my needs, but I encountered a case I don’t understand, similar to the one from one post above.

julia> call = let
              shout() = stop()
              call() = shout()
call (generic function with 1 method)

julia> methods(call).ms[1]
(::var"#call#59")() in Main at REPL[71]:3

julia> methods(call).ms[1].sig.parameters[1].body

julia> methods(call).ms[1].sig.parameters[1].body.instance
ERROR: UndefRefError: access to undefined reference
 [1] getproperty(::Type, ::Symbol) at ./Base.jl:15

What’s going on here? It looks like the function type is not a singleton type, because it’s referring to shout — even though conceptually it could be a singleton, right?

Is there any way to go from method to function in this case?

No. Because shout is a local variable.

1 Like