# Revealing the definition of an anonymous function

**URL:** <https://discourse.julialang.org/t/revealing-the-definition-of-an-anonymous-function/2831>\
**Category:** General Usage\
**Created:** [March 23, 2017, 2:08am UTC](https://discourse.julialang.org/t/revealing-the-definition-of-an-anonymous-function/2831 "2017-03-23T02:08:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Post date:** [March 23, 2017, 2:08am UTC](https://discourse.julialang.org/t/revealing-the-definition-of-an-anonymous-function/2831/1 "2017-03-23T02:08:44Z")

</div>

When an anonymous function is assigned to a variable, I wonder if there is a way to retrieve the definition of the anonymous function from the variable.

Consider the following scenario. Let’s say I have an array of anonymous functions:

```julia
julia> farray = [x->2x, x->√x, x->x^2]
3-element Array{Function,1}:
 #1
 #2
 #3

```

Later I retrieve these functions from the array and evaluate them, but one function produces an error:

```julia
julia> for f = farray
           f(-1)
       end
ERROR: DomainError:
Stacktrace:
 [1] (::##2#5)(::Int64) at ./REPL[0]:1
 [2] macro expansion at ./REPL[1]:2 [inlined]
 [3] anonymous at ./<missing>:?

```

To figure out which function is the culprit, I print out the function information before evaluation:

```julia
julia> for f = farray
           println("f = $f")
           f(-1)
       end
f = #1
f = #2
ERROR: DomainError:
Stacktrace:
 [1] (::##2#5)(::Int64) at ./REPL[0]:1
 [2] macro expansion at ./REPL[2]:3 [inlined]
 [3] anonymous at ./<missing>:?

```

The result shows that the culprit is the anonymous function tagged with `#2`, but from this it is difficult to figure out what the function is. Is there a way to retrieve the definition of the function `#2` here?

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [March 23, 2017, 3:02am UTC](https://discourse.julialang.org/t/revealing-the-definition-of-an-anonymous-function/2831/2 "2017-03-23T03:02:30Z")

</div>

For a simple case like your example:

```julia
julia> @code_lowered(farray[2](-1.0))
LambdaInfo template for (::##4#7)(x) at REPL[4]:1
:(begin 
        nothing
        return √x
    end)

```

---

<div class="post-metadata">

**Author:** ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Post date:** [March 23, 2017, 3:14am UTC](https://discourse.julialang.org/t/revealing-the-definition-of-an-anonymous-function/2831/3 "2017-03-23T03:14:12Z")

</div>

Great! Thanks a lot!!

For completeness, I was able to printout the `@code_lowered` outputs inside the for loop as follows:

```julia
julia> for f = farray
           println("$(@code_lowered f(-1))")
           f(-1)
       end
CodeInfo(:(begin
        nothing
        return 2 * x
    end))
CodeInfo(:(begin
        nothing
        return √x
    end))
ERROR: DomainError:
Stacktrace:
 [1] (::##18#21)(::Int64) at ./REPL[42]:1
 [2] macro expansion at ./REPL[46]:3 [inlined]
 [3] anonymous at ./<missing>:?

```

From this result, we can see that the `√x` is the function that raises the error.
