# Is there a way to get the full definition of a function as String?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-get-the-full-definition-of-a-function-as-string/15306>\
**Category:** General Usage\
**Created:** [September 21, 2018, 7:03pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-full-definition-of-a-function-as-string/15306 "2018-09-21T19:03:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [September 21, 2018, 7:03pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-full-definition-of-a-function-as-string/15306/1 "2018-09-21T19:03:16Z")

</div>

Beside having to parse the script file.

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [September 21, 2018, 7:32pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-full-definition-of-a-function-as-string/15306/2 "2018-09-21T19:32:44Z")

</div>

It looks I can call “code\_lowered” on the function and get a CodeInfo, from which I can find the statements of the function. Is that a reliable way?

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [September 21, 2018, 8:54pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-full-definition-of-a-function-as-string/15306/3 "2018-09-21T20:54:01Z")

</div>

I don’t know a way to get the code of a function as `String`. But there are plenty of ways to do similar things:

- As you mentioned the `@code_...` macros give you the code after some compiler processing
- `@which` gives you a link to the source location
- `@less` shows you the code
- `@edit` opens the code in your favorite editor

---

<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:** [September 21, 2018, 9:43pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-full-definition-of-a-function-as-string/15306/4 "2018-09-21T21:43:37Z")

</div>

Rebugger.jl does this (somehow).

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [September 21, 2018, 9:46pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-full-definition-of-a-function-as-string/15306/5 "2018-09-21T21:46:11Z")

</div>

You wouldn’t recommend using CodeIndo?

---

<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:** [September 21, 2018, 10:14pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-full-definition-of-a-function-as-string/15306/6 "2018-09-21T22:14:33Z")

</div>

CodeInfo is the type that is returned by eg @code\_llvm.

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [September 21, 2018, 10:17pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-full-definition-of-a-function-as-string/15306/7 "2018-09-21T22:17:06Z")

</div>

So, what exactly are you saying?

---

<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:** [September 21, 2018, 10:59pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-full-definition-of-a-function-as-string/15306/8 "2018-09-21T22:59:53Z")

</div>

Yes code\_lowered is a good option, but as its same suggests it returns a lower-level representation of the function, not the source code as it is written.

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [September 22, 2018, 3:14pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-get-the-full-definition-of-a-function-as-string/15306/9 "2018-09-22T15:14:17Z")

</div>

Try Espresso.funexpr() or MacroTools.macro\_form() which Espresso calls internally. They try to find source file of a functions first, and when it’s not available, reconstruct source from lowered code. For me it works in 85-95% of cases, failing mostly on non-trivially defined functions like the ones generated from macros.
