# How to get a function instance from a function type?

**URL:** <https://discourse.julialang.org/t/how-to-get-a-function-instance-from-a-function-type/79585>\
**Category:** Internals & Design\
**Tags:** type, function\
**Created:** [April 17, 2022, 1:52am UTC](https://discourse.julialang.org/t/how-to-get-a-function-instance-from-a-function-type/79585 "2022-04-17T01:52:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![o314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/o314/32/252_2.png) [@o314](https://discourse.julialang.org/u/o314)\
**Post date:** [April 17, 2022, 1:52am UTC](https://discourse.julialang.org/t/how-to-get-a-function-instance-from-a-function-type/79585/1 "2022-04-17T01:52:09Z")

</div>

How can we get a function instance from a function type eg.  
let’s suppose that we def `ft = typeof(abs)` , how can we get `abs` again ?

Currently, i have the following code that runs

```julia
ft = typeof(abs)

using Test
@test abs == getfield(@ __MODULE__ , ft.name.mt.name)

```

But i don’t want to rely on something that seems so much awkward.  
Is there something better that can be safely use ?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [April 17, 2022, 3:32am UTC](https://discourse.julialang.org/t/how-to-get-a-function-instance-from-a-function-type/79585/2 "2022-04-17T03:32:08Z")

</div>

No, because not all functions have a one-to-one relationship with their types. For instance,

```julia
f = let x = 1
   y -> x + y
end

```

captures runtime data and cannot be reconstructed from it’s type alone.

That said, in the case of functions which **are** singleton instances, you could do

```julia
if isdefined(tf, :instance)
    ft.instance
else
    error("This type does not have a single instance")
end

```

---

<div class="post-metadata">

**Author:** ![o314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/o314/32/252_2.png) [@o314](https://discourse.julialang.org/u/o314)\
**Post date:** [April 17, 2022, 4:14am UTC](https://discourse.julialang.org/t/how-to-get-a-function-instance-from-a-function-type/79585/3 "2022-04-17T04:14:31Z")

</div>

I was looking for an instance fld like for enum . And it was here 🙂  
Thanks for pointing out it quickly !

BTW

```julia
using Test
@test f.x == 1 # agree, one needs data out of typeof(f)
@test length(typeof(f).name.mt |> Base.MethodList) == 1 # but will be true forever

```

kind of `all(typeof(f).name.mt) do m; m.x==1 end`  
it’s more a question of storage location than of multiple instance isn’t it ?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [April 17, 2022, 4:53am UTC](https://discourse.julialang.org/t/how-to-get-a-function-instance-from-a-function-type/79585/4 "2022-04-17T04:53:17Z")

</div>

> [@o314](#):
>
> `@test length(typeof(f).name.mt |> Base.MethodList) == 1 # but will be true forever`

Nope 🙂

```julia
julia> f = let x = 1
          y -> x + y
       end
#5 (generic function with 1 method)

julia> (f::typeof(f))(x, y) = x + y - f.x

julia> length(typeof(f).name.mt |> Base.MethodList)
2

```

Regarding this:

> [@o314](#):
>
> ``  
> it’s more a question of storage location than of multiple instance isn’t it ?

That’s not what’s meant by instance here. Instance is always a matter of storage, never about the number of methods. E.g.

```julia
julia> length(methods(+))
208

```

Yet, `+` only has one single instance:

```julia
julia> typeof(+).instance
+ (generic function with 208 methods)

```

Rather, what’s important here is that I can make new copies of `f` that have the **same** type, but are distinct:

```julia
julia> macro new(T, args...)
           Expr(:new, T, args...)
       end
@new (macro with 1 method)

julia> f2 = @new typeof(f) 2
#5 (generic function with 2 methods)

julia> f2.x
2

julia> typeof(f2) == typeof(f)
true

julia> f2 == f # <--- This is the really important part
false

```

Now, in reality, you probably shouldn’t be doing this with closures, it’s not really something you’re intended to be doing, but keep in mind that any user defined struct could be a function if you want:

```julia
julia> struct Foo <: Function
           x::Int
       end

julia> (foo::Foo)(x) = foo.x + x

julia> Foo(1)(2)
3

```
