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.


There is:

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


Slightly simpler:

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


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) 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...)
broadcast (generic function with 99 methods)

julia> :a .+ :b .* :c
        #temp#@_5 = #temp#@_3 * #temp#@_4
        return #temp#@_2 + #temp#@_5

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)))
        return a + b * c