# Get an anonymous function as a string

**URL:** <https://discourse.julialang.org/t/get-an-anonymous-function-as-a-string/27523>\
**Category:** New to Julia\
**Created:** [August 14, 2019, 9:01am UTC](https://discourse.julialang.org/t/get-an-anonymous-function-as-a-string/27523 "2019-08-14T09:01:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marco-Congedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco-congedo/32/7321_2.png) [@Marco-Congedo](https://discourse.julialang.org/u/Marco-Congedo)\
**Post date:** [August 14, 2019, 9:01am UTC](https://discourse.julialang.org/t/get-an-anonymous-function-as-a-string/27523/1 "2019-08-14T09:01:29Z")

</div>

`string` returns the name of a function as a string:

julia\> string(sqrt)  
“sqrt”

but that does not work the same for an anonymous function:

julia\> string(x-\>x^3)  
“getfield(Main, Symbol("##9#10"))()”

How can i get an anonymous function as a string, i.e., “x-\>x^3” in the example above?

P.S. i need to do it within a function to which the anonymous function is passed as an argument.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [August 14, 2019, 9:09am UTC](https://discourse.julialang.org/t/get-an-anonymous-function-as-a-string/27523/2 "2019-08-14T09:09:36Z")

</div>

My understanding is that Julia is showing you the same thing in each case, only the “name” of the anonymous function is an automatically generated symbol (to give it a unique name). To try to get the behavior you want, maybe try [https://github.com/MasonProtter/LegibleLambdas.jl](https://github.com/MasonProtter/LegibleLambdas.jl) (but you have to define the anonymous functions with a macro).

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [August 14, 2019, 9:17am UTC](https://discourse.julialang.org/t/get-an-anonymous-function-as-a-string/27523/3 "2019-08-14T09:17:02Z")

</div>

Sorry, just saw that you want to get the string representation from an anonymous function that was passed as an argument. Then I guess you need to do some kind of introspection to see how it was defined or what code it executes. Maybe [https://github.com/timholy/CodeTracking.jl/](https://github.com/timholy/CodeTracking.jl/) can help?

---

<div class="post-metadata">

**Author:** ![Marco-Congedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco-congedo/32/7321_2.png) [@Marco-Congedo](https://discourse.julialang.org/u/Marco-Congedo)\
**Post date:** [August 14, 2019, 9:41am UTC](https://discourse.julialang.org/t/get-an-anonymous-function-as-a-string/27523/4 "2019-08-14T09:41:49Z")

</div>

Indeed LegibleLambdas does not do what i need:

julia\> using LegibleLambdas

julia\> f=x-\>x^3  
#9 (generic function with 1 method)

julia\> g=@λ(f)  
ERROR: LoadError: MethodError: no method matching parse\_lambda(::Symbol)  
Closest candidates are: parse\_lambda(::Expr) at C:\Users\congedom.julia\packages\LegibleLambdas\kpxjE\src\local.jl:11  
Stacktrace: [1] @λ(::LineNumberNode, ::Module, ::Any) at C:\Users\congedom.julia\packages\LegibleLambdas\kpxjE\src\local.jl:2  
in expression starting at none:1

I will check CodeTracking. Thanks!

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [August 14, 2019, 11:44am UTC](https://discourse.julialang.org/t/get-an-anonymous-function-as-a-string/27523/5 "2019-08-14T11:44:31Z")

</div>

The julia language has no real distinction between anonymous and named generic functions.

Especially, functions don’t have source code, methods have:

```julia
julia> a=x->x^3;

julia> (::typeof(a))(x,y)=x+y;

julia> a
#3 (generic function with 2 methods)

```

There are many ways of defining methods. What would you want to do with the string? What if the thing was defined by a macro or a generated function? Do you want the generator or the generated AST / Expr? Oh, but generated functions can also return IR, so the string could also be printed IR instead of printed AST.

---

<div class="post-metadata">

**Author:** ![Marco-Congedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco-congedo/32/7321_2.png) [@Marco-Congedo](https://discourse.julialang.org/u/Marco-Congedo)\
**Post date:** [August 14, 2019, 12:14pm UTC](https://discourse.julialang.org/t/get-an-anonymous-function-as-a-string/27523/6 "2019-08-14T12:14:05Z")

</div>

I have a constructor of a struct taking (among other things) a function as an argument. All i need is a string to fill a field of the struct so that the user may know what function has been applied on construction.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [August 14, 2019, 12:23pm UTC](https://discourse.julialang.org/t/get-an-anonymous-function-as-a-string/27523/7 "2019-08-14T12:23:02Z")

</div>

The natural way of doing that would be to store a julia object instead of a string:

```julia
struct foo{#= type params =#}
#other_fields
constructor_fun
end

```

and plug in either the constructor function (`fun`) or its type (`typeof(fun)`). The two behave different for closures: If your constructor function is a closure, then one version will store just what kind of closure was passed, and the other will store the closed over variables as well (with corresponding memory cost).

Note that `constructor_fun` is intentionally untyped, instead of parametric. This allows you to store the objects in homogeneous arrays, and only code that actually uses `constructor_fun` will be slowed down (which should be rare).

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 14, 2019, 12:29pm UTC](https://discourse.julialang.org/t/get-an-anonymous-function-as-a-string/27523/8 "2019-08-14T12:29:40Z")

</div>

> [@Marco-Congedo](#):
>
> P.S. i need to do it within a function to which the anonymous function is passed as an argument.

What are you trying to do? If you are trying to manipulate functions as strings then you should probably re-think your approach.

There has been some discussion of keeping the function source AST around, at least in interactive code, for display purposes if nothing else:

> <https://github.com/JuliaLang/julia/issues/2625>
>
> Julia has a lot of useful REPL things like \`methods\` and \`help\`.
> However, I stil…l end up needing to just look at the code to see what it's doing.
> It would be cool to be able to do:
> 
> \`\`\`
> julia\> methods(base)
> \# methods for generic function base
> base(base::Integer,n::Integer,pad::Integer) at intfuncs.jl:290
> base(symbols::Array{Uint8,N},n::Integer,p::Integer) at intfuncs.jl:291
> base(base\_or\_symbols::Union(Integer,Array{Uint8,N}),n::Integer) at intfuncs.jl:292
> 
> julia\> implementation(base,1)
> base(base::Integer, n::Integer, pad::Integer) = \_base(dig\_syms,int(base),unsigned(abs(n)),pad,n\<0)
> 
> julia\> implementation(base,3)
> base(base\_or\_symbols::Union(Integer,Array{Uint8}), n::Integer) = base(base\_or\_symbols, n, 1)
> \`\`\`
> 
> It's kind of like the jump-to code stuff in some IDEs, but in the REPL, and only showing the one function.
> This obviously has limitations, since you can't just scroll up and see the implementation of \`\_base\` or search for the definition of \`dig\_syms\` in that file, but it does let you see what the default values are.
> 
> You can already see the signature for \`\_base\`, which makes the implementations of \`base\` more meaningful. (without needing to switch from the REPL to a text editor)
> 
> \`\`\`
> julia\> methods(Base.\_base)
> \# methods for generic function \_base
> \_base(symbols::Array{Uint8,N},b::Int32,x::Unsigned,pad::Int32,neg::Bool) at intfuncs.jl:278
> \`\`\`
> 
> Considering that the line numbers/files are already included in the output of \`methods\`, it seems like it should be straightforward to grab the appropriate lines of code from the file.

---

<div class="post-metadata">

**Author:** ![Marco-Congedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco-congedo/32/7321_2.png) [@Marco-Congedo](https://discourse.julialang.org/u/Marco-Congedo)\
**Post date:** [August 14, 2019, 1:57pm UTC](https://discourse.julialang.org/t/get-an-anonymous-function-as-a-string/27523/9 "2019-08-14T13:57:57Z")

</div>

Thank you guys for pointing me to these posts.

This seems to answer my question:  
[4089/2](https://discourse.julialang.org/t/nice-way-to-display-functions/4089/3),
