# "code reflection cannot be used from generated functions"

**URL:** https://discourse.julialang.org/t/code-reflection-cannot-be-used-from-generated-functions/21678
**Category:** General Usage
**Tags:** question
**Created:** [March 9, 2019, 2:27pm UTC](https://discourse.julialang.org/t/code-reflection-cannot-be-used-from-generated-functions/21678 "2019-03-09T14:27:36Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [March 9, 2019, 2:27pm UTC](https://discourse.julialang.org/t/code-reflection-cannot-be-used-from-generated-functions/21678/1 "2019-03-09T14:27:36Z")

</div>

I just got to love `code_typed` as it can compile-infer whether a function will fail because of MethodNotFound Error

However my usecase requires this as part of a generated function… which is why I run into the following error

```julia
code reflection cannot be used from generated functions

```

Looking into the code of `code_typed` I find the following hard-coded check

```julia
function code_typed(@nospecialize(f), @nospecialize(types=Tuple);
                    optimize=true, debuginfo::Symbol=:default,
                    world = ccall(:jl_get_world_counter, UInt, ()),
                    params = Core.Compiler.Params(world))
    ccall(:jl_is_in_pure_context, Bool, ()) && error("code reflection cannot be used from generated functions")

```

So there is indeed a hard-coded check whether this function is called in a generated context or not.

Why is this is not supported? It feels enormously like it should be possible to work, as only Type information and module information is needed.  
Does someone know a workaround? I really really need generated functions here and would like to use code\_typed for its inference support.

---

<div class="post-metadata">

### Author: ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)
#### Post date: [March 9, 2019, 2:35pm UTC](https://discourse.julialang.org/t/code-reflection-cannot-be-used-from-generated-functions/21678/2 "2019-03-09T14:35:33Z")

</div>

Just found a solution.

I only needed the inferred returntype, whether it is Union{} or not.  
This can also be done with `Core.Compiler.return_type` which successfully runs in generated functions!

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [March 12, 2019, 2:26am UTC](https://discourse.julialang.org/t/code-reflection-cannot-be-used-from-generated-functions/21678/3 "2019-03-12T02:26:34Z")

</div>

You can just use that at runtime, then when you fix the code, Revise.jl can pickup the correction and you don’t need to restart Julia.

```julia
RT = Base.promote_op(f, argT...) # preferred name for calling to `Core.Compiler.return_type`
RT === Union{} && @warn "call to F is statically known to fail"

```
