How to get the code behind an anonymous function represented as Symbol("##...")

I’m extensively using code like this:

route("/") do
  "Hello World"
end

which gets custom printed as:

[GET] / => getfield(Main, Symbol("##3#4"))()

It would be a lot more usable and informative if I could actually retrieve the code behind Symbol("##3#4")). Is this possible? Thanks!

1 Like

Is this what you’re thinking?

julia> f = x -> 2x
#7 (generic function with 1 method)

julia> m = first(methods(f))
(::getfield(Main, Symbol("##7#8")))(x) in Main at REPL[1]:1

julia> using CodeTracking

julia> whereis(m)[1]
"REPL[1]"

julia> CodeTracking.src_from_file_or_REPL(whereis(m)[1])
"f = x -> 2x"

Edit: CodeTracking basically has this in its README. Using just exported functions, it can instead be written,

julia> definition(String, @which f(1.0))[1]
"f = x -> 2"
6 Likes

That’s pretty cool, thank you!