# Get function object from method

**URL:** https://discourse.julialang.org/t/get-function-object-from-method/6524
**Category:** Internals & Design
**Created:** [October 18, 2017, 2:41pm UTC](https://discourse.julialang.org/t/get-function-object-from-method/6524 "2017-10-18T14:41:21Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [October 18, 2017, 2:41pm UTC](https://discourse.julialang.org/t/get-function-object-from-method/6524/1 "2017-10-18T14:41:21Z")

</div>

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

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [October 18, 2017, 2:49pm UTC](https://discourse.julialang.org/t/get-function-object-from-method/6524/2 "2017-10-18T14:49:26Z")

</div>

The first type in sig

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

julia> typeof(m)
Method

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.

---

<div class="post-metadata">

### Author: ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)
#### Post date: [January 22, 2018, 12:22am UTC](https://discourse.julialang.org/t/get-function-object-from-method/6524/3 "2018-01-22T00:22:54Z")

</div>

One possible way to get the function itself:

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

```

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

```julia
#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

```

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [January 22, 2018, 12:57am UTC](https://discourse.julialang.org/t/get-function-object-from-method/6524/4 "2018-01-22T00:57:10Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)
#### Post date: [January 24, 2018, 4:39am UTC](https://discourse.julialang.org/t/get-function-object-from-method/6524/5 "2018-01-24T04:39:12Z")

</div>

> [@yuyichao](#):
>
> x -\> x + a is NOT a closure

You are correct! My mistake.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [January 24, 2018, 2:12pm UTC](https://discourse.julialang.org/t/get-function-object-from-method/6524/6 "2018-01-24T14:12:06Z")

</div>

From private message:

```julia
let
    state = 0
    global counter
    counter(x) = state = state + x + 1
end

```

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 `counter`s. Also note that I believe this syntax is deprecated on master and will require explicit splicing.

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [December 18, 2019, 10:33am UTC](https://discourse.julialang.org/t/get-function-object-from-method/6524/7 "2019-12-18T10:33:00Z")

</div>

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
julia> call = let
              shout() = stop()
              call() = shout()
              call
              end
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
var"#call#59"{shout}

julia> methods(call).ms[1].sig.parameters[1].body.instance
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [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?

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [December 19, 2019, 4:04am UTC](https://discourse.julialang.org/t/get-function-object-from-method/6524/8 "2019-12-19T04:04:11Z")

</div>

> [@cstjean](#):
>
> even though conceptually it could be a singleton, right?

No. Because `shout` is a local variable.
