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

**URL:** https://discourse.julialang.org/t/is-there-a-way-to-get-the-expression-object-for-a-function/2660
**Category:** General Usage
**Created:** [March 14, 2017, 11:12am UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-expression-object-for-a-function/2660 "2017-03-14T11:12:39Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [March 14, 2017, 11:12am UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-expression-object-for-a-function/2660/1 "2017-03-14T11:12:39Z")

</div>

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,

```julia
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 `op`s which are a composition of many operations.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [March 14, 2017, 11:23am UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-expression-object-for-a-function/2660/2 "2017-03-14T11:23:11Z")

</div>

There is:

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

```

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [March 14, 2017, 11:32am UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-expression-object-for-a-function/2660/3 "2017-03-14T11:32:32Z")

</div>

Slightly simpler:

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

```

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [March 14, 2017, 12:50pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-expression-object-for-a-function/2660/4 "2017-03-14T12:50:58Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [March 17, 2017, 11:29am UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-expression-object-for-a-function/2660/5 "2017-03-17T11:29:58Z")

</div>

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
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
julia> Base.uncompressed_ast(first(methods((a,b,c)->a+b*c)))
CodeInfo(:(begin 
        nothing
        return a + b * c
    end))

```
