# When compiler decide it will use generated function

**URL:** <https://discourse.julialang.org/t/when-compiler-decide-it-will-use-generated-function/71636>\
**Category:** Internals & Design\
**Created:** [November 17, 2021, 6:02am UTC](https://discourse.julialang.org/t/when-compiler-decide-it-will-use-generated-function/71636 "2021-11-17T06:02:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [November 17, 2021, 6:02am UTC](https://discourse.julialang.org/t/when-compiler-decide-it-will-use-generated-function/71636/1 "2021-11-17T06:02:25Z")

</div>

Hello,

I am reading and experimenting with generated functions. The docs mention to give the compiler an option to use or not use the generated function using the construction

```julia
function foo(x)
  if @generated
      :(x+x)
  else 
     x + x
  end
end

```

The documentation is not very clear about at which conditions the compiler decides to use it and when not? I would like to understand these conditions, because I have just found that @generated functions can be sometimes used to create a nicer code for Zygote (manually unrolling `map` for example).

Thanks for an answer,  
Tomas

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 17, 2021, 6:07am UTC](https://discourse.julialang.org/t/when-compiler-decide-it-will-use-generated-function/71636/2 "2021-11-17T06:07:53Z")

</div>

The reason the compiler isn’t clear about this is that it can do whatever it feels like, and we don’t want to document internal behavior (because if we do, people will rely on it).

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 17, 2021, 8:22am UTC](https://discourse.julialang.org/t/when-compiler-decide-it-will-use-generated-function/71636/3 "2021-11-17T08:22:01Z")

</div>

It was introduced in [https://github.com/JuliaLang/julia/pull/23168](https://github.com/JuliaLang/julia/pull/23168), you might find something of use in there.

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [November 17, 2021, 9:31am UTC](https://discourse.julialang.org/t/when-compiler-decide-it-will-use-generated-function/71636/4 "2021-11-17T09:31:36Z")

</div>

Thanks a lot,

This was a good reading.

P.S. I do not know, which answer should I mark as a solution. Jeff Bezanson’s last comment of that pull request was interesteting. Also reading about possible tests of both was great.
