# How to get anonymous function from its type?

**URL:** <https://discourse.julialang.org/t/how-to-get-anonymous-function-from-its-type/53843>\
**Category:** General Usage\
**Created:** [January 23, 2021, 8:37pm UTC](https://discourse.julialang.org/t/how-to-get-anonymous-function-from-its-type/53843 "2021-01-23T20:37:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [January 23, 2021, 8:37pm UTC](https://discourse.julialang.org/t/how-to-get-anonymous-function-from-its-type/53843/1 "2021-01-23T20:37:27Z")

</div>

```julia
f = x -> x + 1
T = typeof(f)
# ==> var"#2#4"

```

Given a type `T` of an anonymous function, can I get that function? I.e. given `var#"2#4"` get object `f`?

I get the type `T` from the lowered representation of code (actually, from IRTools, but it’s nearly the same in this context). E.g.:

```julia
@code_lowered (xs -> [x + 1 for x in xs])(rand(5))
# ==> CodeInfo(
# ==> 1 ─ #2 = %new(Main.:(var"#2#4"))
# ==> │ %2 = #2
# ==> │ %3 = Base.Generator(%2, xs)
# ==> │ %4 = Base.collect(%3)
# ==> └── return %4
)

```

In IR, we have a special instruction `%new` which constructs the function, but I can’t find a language or compiler feature to do it from my code.

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [January 23, 2021, 8:45pm UTC](https://discourse.julialang.org/t/how-to-get-anonymous-function-from-its-type/53843/2 "2021-01-23T20:45:04Z")

</div>

For singleton types (such as anonymous functions which aren’t closures), you can access the instance from the `instance` field:

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

julia> T = typeof(f)
var"#1#2"

julia> T.instance
#1 (generic function with 1 method)

```

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [February 21, 2021, 8:37am UTC](https://discourse.julialang.org/t/how-to-get-anonymous-function-from-its-type/53843/3 "2021-02-21T08:37:59Z")

</div>

Because of context switch I didn’t realize this approach doesn’t work for `UnionAll` types like in the example with the generator:

```julia
@code_lowered (xs -> [x + 1 for x in xs])(rand(5))
# ==> CodeInfo(
# ==> 1 ─ #130 = %new(Main.:(var"#130#132"))
# ==> │ %2 = #130
# ==> │ %3 = Base.Generator(%2, xs)
# ==> │ %4 = Base.collect(%3)
# ==> └── return %4
# ==> )
T = var"#130#132"
T.instance
# ==> ERROR: type UnionAll has no field instance
T.body.instance
# ==> ERROR: UndefRefError: access to undefined reference
T{:x}.instance
# ==> ERROR: UndefRefError: access to undefined reference

```

Any directions on how to refer to the underlying function (essentially, `x + 1`) in this example?

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [April 26, 2021, 11:16pm UTC](https://discourse.julialang.org/t/how-to-get-anonymous-function-from-its-type/53843/4 "2021-04-26T23:16:01Z")

</div>

After many experiments I came up with the following utility function which can instantiate _any_ objects including anonymous functions:

```julia
@generated function __new__ (T, args...)
    return Expr(:splatnew, :T, :args)
end

```

Example:

```julia
julia> @code_lowered (xs -> [x + 1 for x in xs])(rand(5))
CodeInfo(
1 ─ #58 = %new(Main.:(var"#58#60"))
│ %2 = #58
│ %3 = Base.Generator(%2, xs)
│ %4 = Base.collect(%3)
└── return %4
)

julia> T = var"#58#60"
var"#58#60"

julia> f = __new__ (T)
#58 (generic function with 1 method)

julia> f(1)
2

```
