# Can I detect if code is being run from a generated function?

**URL:** <https://discourse.julialang.org/t/can-i-detect-if-code-is-being-run-from-a-generated-function/62241>\
**Category:** General Usage\
**Created:** [June 2, 2021, 1:47am UTC](https://discourse.julialang.org/t/can-i-detect-if-code-is-being-run-from-a-generated-function/62241 "2021-06-02T01:47:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [June 2, 2021, 1:47am UTC](https://discourse.julialang.org/t/can-i-detect-if-code-is-being-run-from-a-generated-function/62241/1 "2021-06-02T01:47:03Z")

</div>

Is there any way to detect if some code is being run inside a generated function? Something like:

```julia
function foo()
    if being_called_from_a_generated_function()
        # do something
    else
        # do it slightly differently
    end
end

@generated function bar()
    foo() # should take the "do something" branch
end

@generated function baz()
    quote
        foo() # should take the "do it slightly differently" branch
    end
end

```

Basically I have a `foo` with an efficient implementation but which is illegal inside generated functions, but `foo` can still work from inside generated functions as long as I do things slightly less efficiently to stay within the rules of what is allowed there, and I’d like to be able to have `foo` work in either case.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [June 2, 2021, 2:21am UTC](https://discourse.julialang.org/t/can-i-detect-if-code-is-being-run-from-a-generated-function/62241/2 "2021-06-02T02:21:27Z")

</div>

if u r using generated, function you can control which version gets called right?

Not sure why calling `foo_inside_generated` is not a solution instead of detection?

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [June 2, 2021, 2:22am UTC](https://discourse.julialang.org/t/can-i-detect-if-code-is-being-run-from-a-generated-function/62241/3 "2021-06-02T02:22:41Z")

</div>

> [@xiaodai](#):
>
> you can control which version gets called right?

I can’t, this is up to the users of this function, and I don’t want to force them having to think about this.

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [June 2, 2021, 2:33am UTC](https://discourse.julialang.org/t/can-i-detect-if-code-is-being-run-from-a-generated-function/62241/4 "2021-06-02T02:33:48Z")

</div>

Ahh, found it, its `jl_is_in_pure_context`:

```julia
julia> foo() = ccall(:jl_is_in_pure_context, Cint, ()) == 1 ? "generated" : "not generated";

julia> @generated bar() = foo();

julia> @generated baz() = :(foo());

julia> bar()
"generated"

julia> baz()
"not generated"

```
