# Generated functions and reflection

**URL:** <https://discourse.julialang.org/t/generated-functions-and-reflection/111301>\
**Category:** Internals & Design\
**Created:** [March 7, 2024, 2:39pm UTC](https://discourse.julialang.org/t/generated-functions-and-reflection/111301 "2024-03-07T14:39:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![willtebbutt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willtebbutt/32/6790_2.png) [@willtebbutt](https://discourse.julialang.org/u/willtebbutt)\
**Post date:** [March 7, 2024, 2:39pm UTC](https://discourse.julialang.org/t/generated-functions-and-reflection/111301/1 "2024-03-07T14:39:50Z")

</div>

Why is it the case that reflection is not permitted from within generated functions? (I know that e.g. Zygote does it, but I get the impression that’s frowned upon). I had a search through old posts but can’t find a clear answer. If I’ve missed a well-known post, I would appreciate a pointer towards it!

My confusion stems from the fact that I cannot see why is not safe to call e.g. `Base.code_ircode_by_type(sig; world, interp, optimize_until)` from a generated function (using the version of a generated function where you get access to the world age). I guess that I can’t see why the returned `IRCode` ought not to be a pure function of these arguments. Or does constructing a NativeInterpreter involve making use of something global and stateful?

---

<div class="post-metadata">

**Author:** ![apieum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apieum/32/2928_2.png) [@apieum](https://discourse.julialang.org/u/apieum)\
**Post date:** [March 7, 2024, 9:49pm UTC](https://discourse.julialang.org/t/generated-functions-and-reflection/111301/2 "2024-03-07T21:49:50Z")

</div>

Hi, maybe this one can help you: [Constraints for `@generated` function - #2 by cscherrer](https://discourse.julialang.org/t/constraints-for-generated-function/27967/2)

Also this one: [Eval of Julia code · The Julia Language](https://docs.julialang.org/en/v1/devdocs/eval/#dev-type-inference)

“a generated function gets expanded at a time when the types of the arguments are known”, `code_ircode_by_type` calls `typeinf_ircode` ([julia/base/compiler/typeinfer.jl at master · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/master/base/compiler/typeinfer.jl#L975) ) so I guess the types of the arguments are not known at this time and the function is not expanded. Someone with a better understanding than me can probably confirm.
