# Get function as expr

**URL:** <https://discourse.julialang.org/t/get-function-as-expr/51886>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [December 15, 2020, 5:20pm UTC](https://discourse.julialang.org/t/get-function-as-expr/51886 "2020-12-15T17:20:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Volker\_Weissmann](https://avatars.discourse-cdn.com/v4/letter/v/c2a13f/32.png) [@Volker\_Weissmann](https://discourse.julialang.org/u/Volker_Weissmann)\
**Post date:** [December 15, 2020, 5:20pm UTC](https://discourse.julialang.org/t/get-function-as-expr/51886/1 "2020-12-15T17:20:51Z")

</div>

My question is the same as [Is there a way to get the expression object for a function?](https://discourse.julialang.org/t/is-there-a-way-to-get-the-expression-object-for-a-function/2660) , but the old answer does not work in new julia versions.  
Lets say you defined method:

```julia
function func()
    return 17
end

```

I know that you ca use `@code_lowered func()` `@code_typed func()` `@code_llvm func()` or `@code_native func()` to get different represantations of this code. But is there a way to get an `Expr`? I need a macro/function that takes func() as an argument and returns:

```julia
quote
    function func()
        return 17
    end
end

```

Note: putting an `@mymacro` in front of `function func()` is not an option, because it needs to work on existing code.

---

<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:** [December 15, 2020, 5:26pm UTC](https://discourse.julialang.org/t/get-function-as-expr/51886/2 "2020-12-15T17:26:16Z")

</div>

If the code has no control flow then you can use ModelingToolkit.jl

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 15, 2020, 5:27pm UTC](https://discourse.julialang.org/t/get-function-as-expr/51886/3 "2020-12-15T17:27:13Z")

</div>

There is [https://github.com/timholy/CodeTracking.jl](https://github.com/timholy/CodeTracking.jl), but there are many situations where a function simply has no representation as an Expr.

---

<div class="post-metadata">

**Author:** ![Volker\_Weissmann](https://avatars.discourse-cdn.com/v4/letter/v/c2a13f/32.png) [@Volker\_Weissmann](https://discourse.julialang.org/u/Volker_Weissmann)\
**Post date:** [December 15, 2020, 5:35pm UTC](https://discourse.julialang.org/t/get-function-as-expr/51886/4 "2020-12-15T17:35:11Z")

</div>

Which functions do not have a representation as an Expr.

---

<div class="post-metadata">

**Author:** ![Volker\_Weissmann](https://avatars.discourse-cdn.com/v4/letter/v/c2a13f/32.png) [@Volker\_Weissmann](https://discourse.julialang.org/u/Volker_Weissmann)\
**Post date:** [December 15, 2020, 6:09pm UTC](https://discourse.julialang.org/t/get-function-as-expr/51886/5 "2020-12-15T18:09:52Z")

</div>

Is it possible to use CodeTracking.jl in non interactive scripts?

This:

```julia
using InteractiveUtils
using Revise
using CodeTracking

function func()
return 123
end

println(@code_string func())
println(@code_expr func())

```

outputs:

```julia
function func()
    return 123
end
nothing

```

if I execute it using `julia script.jl`

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 15, 2020, 6:22pm UTC](https://discourse.julialang.org/t/get-function-as-expr/51886/6 "2020-12-15T18:22:07Z")

</div>

> [@Volker\_Weissmann](#):
>
> Which functions do not have a representation as an Expr.

In julia, you can generate a function from the IR directly and still have a valid function. Any function generated in that way won’t have an `Expr` representation. Furthermore, CodeTracking.jl can only be so clever. Most functions generated by macros won’t be recognized or tracked by it.

> [@Volker\_Weissmann](#):
>
> Is it possible to use CodeTracking.jl in non interactive scripts?
> 
> This:
> 
> ```julia
> using InteractiveUtils
> using Revise
> using CodeTracking
> 
> function func()
> return 123
> end
> 
> println(@code_string func())
> println(@code_expr func())
> 
> ```
> 
> outputs:
> 
> ```julia
> function func()
> return 123
> end
> nothing
> 
> ```
> 
> if I execute it using `julia script.jl`

That seems like a bug, but I don’t know. Perhaps open an issue on the github page.
