Is there a way to get the expression object for a function?

Essentially, I’m writing some custom broadcast(op, args...) method and I want to do some fancy logic based on what function op is. Is there a way to turn op back into it’s expression object? Something like,

f(x) = x^2
get_code(f) # would return :(x^2)

Edit: And to clarify, I’m aware you can dispatch on e.g. broadcast(::typeof(*), args...) but I need this to work for more general ops which are a composition of many operations.

2 Likes

There is:

julia> Base.uncompressed_ast(collect(methods(f))[1])
2-element Array{Any,1}:
 nothing         
 :(return _2 ^ 2)
3 Likes

Slightly simpler:

julia> Base.uncompressed_ast(first(methods(f)))
2 Likes

Very cool, thanks guys. And for posterity, you can (in 0.6) apparently also get the expression from just a function type, via Base.uncompressed_ast(typeof(func).name.mt.defs.func). In my case this is nice because I can make broadcast be @generated and thus more efficient.

Quick followup in case anyone knows. I suppose this is more related to how exactly the broadcast function works, but it appears to create an anonymous function with a bunch of temporary variables. For example,

julia> import Base.broadcast

julia> @generated function broadcast(f,args::Symbol...)
           Base.uncompressed_ast(f.name.mt.defs.func)
       end
broadcast (generic function with 99 methods)

julia> :a .+ :b .* :c
CodeInfo(:(begin 
        #temp#@_5 = #temp#@_3 * #temp#@_4
        return #temp#@_2 + #temp#@_5
    end))

Does there perhaps already exist a function in Julia to return this form back into a single expression such as is returned by just defining the function ourselves:

julia> Base.uncompressed_ast(first(methods((a,b,c)->a+b*c)))
CodeInfo(:(begin 
        nothing
        return a + b * c
    end))