# Resolve GlobalRef in a generated function

**URL:** <https://discourse.julialang.org/t/resolve-globalref-in-a-generated-function/43645>\
**Category:** General Usage\
**Created:** [July 24, 2020, 11:25pm UTC](https://discourse.julialang.org/t/resolve-globalref-in-a-generated-function/43645 "2020-07-24T23:25:32Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [July 24, 2020, 11:25pm UTC](https://discourse.julialang.org/t/resolve-globalref-in-a-generated-function/43645/1 "2020-07-24T23:25:32Z")

</div>

I’m using [`@dynamo`](https://fluxml.ai/IRTools.jl/latest/dynamo/) from IRTools to rewrite all calls inside a passed function. There is a set of functions - let’s call them primitives - that I don’t want to rewrite, and thus I need to check if a particular function is in this set _during code transformation_. However:

1. `IRTools.@dynamo` utilizes Julia’s generated functions to let us transform code. As we know, generated functions act during compilation (or rather compilation-evaluation loop).
2. In the transformed code functions are represented as unresolved `GlobalRefs`. E.g. function `+` may be represented as `GlobalRef(Base, :+)` or `GlobalRef(Main, :+)`, or in some other similar way.
3. In normal circumstances I would resolve `GlobalRef` to function instance using `eval`, however `eval` cannot be used in generated functions.

My current approach is to generate all possible `GlobalRef`s that can appear in the code, but it doesn’t seem to be very robust.

Is there a way to get a `GlobalRef(some_module, function_name)` and resolve it a generated function?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [July 24, 2020, 11:38pm UTC](https://discourse.julialang.org/t/resolve-globalref-in-a-generated-function/43645/2 "2020-07-24T23:38:58Z")

</div>

You don’t need `eval` here, you can just use `getfield` or `getproperty`:

```julia
ex = GlobalRef(Base, :+)
getproperty(ex.mod, ex.name)

#+RESULTS:
: + (generic function with 207 methods)

```

---

<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:** [July 24, 2020, 11:45pm UTC](https://discourse.julialang.org/t/resolve-globalref-in-a-generated-function/43645/3 "2020-07-24T23:45:46Z")

</div>

Indeed! I felt it should be very easy and it is. Thanks a lot!
