# How to get the current world age inside a generated function?

**URL:** https://discourse.julialang.org/t/how-to-get-the-current-world-age-inside-a-generated-function/62969
**Category:** General Usage
**Tags:** runtimegeneratedfunc, world\_age
**Created:** [June 15, 2021, 7:56pm UTC](https://discourse.julialang.org/t/how-to-get-the-current-world-age-inside-a-generated-function/62969 "2021-06-15T19:56:25Z")
**Posts on this page:** 6
**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 15, 2021, 7:56pm UTC](https://discourse.julialang.org/t/how-to-get-the-current-world-age-inside-a-generated-function/62969/1 "2021-06-15T19:56:26Z")

</div>

A generated function I’m writing needs to determine if its allowed to call a function its been passed, or if that would result in a world-age error (in which case it would fall back to something else). As part of this, I suppose I need to get the world-age that the generated function is currently running in and pass that to `hasmethod`, but this doesn’t seem to work. Here’s what I’ve tried:

```julia
defined_before() = "before"
@generated function try_call(func)
    if hasmethod(func.instance, (), world=Base.get_world_counter())
        func.instance()
    else
        "cant call"
    end
end
defined_after() = "after"
try_call(defined_before) # returns "before"; good
try_call(defined_after) # should return "cant call", instead MethodErrors

```

Adding a print statement around the `Base.get_world_counter()` inside the generated function reveals that it returns the “current” world-age after everything’s been defined, rather than the world-age inside the generated function (ie I’m guessing this is not what `get_world_counter` is meant for):

```julia
Int(Base.get_world_counter()) = 29689
ERROR: LoadError: MethodError: no method matching defined_after()
The applicable method may be too new: running in world age 29688, while current world is 29689.

```

So is there any way to do this, without just trying to call `func` and catching the potential MethodError? Alternatively, is there a better way to determine if a function is callable in the current world age?

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [June 15, 2021, 10:37pm UTC](https://discourse.julialang.org/t/how-to-get-the-current-world-age-inside-a-generated-function/62969/2 "2021-06-15T22:37:39Z")

</div>

You are not allowed to do this in a generated function. Its world age is always frozen to the world age it is defined in, since it would otherwise need to be recompiled every time any new method is defined.

---

<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 15, 2021, 10:39pm UTC](https://discourse.julialang.org/t/how-to-get-the-current-world-age-inside-a-generated-function/62969/3 "2021-06-15T22:39:21Z")

</div>

Not allowed to do what? Even just check whether the call is allowed by the current world-age?

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [June 15, 2021, 10:41pm UTC](https://discourse.julialang.org/t/how-to-get-the-current-world-age-inside-a-generated-function/62969/4 "2021-06-15T22:41:18Z")

</div>

Yes, you can’t ask for the world age a generated function is called in or the current world age.

---

<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 15, 2021, 10:53pm UTC](https://discourse.julialang.org/t/how-to-get-the-current-world-age-inside-a-generated-function/62969/5 "2021-06-15T22:53:15Z")

</div>

I see, thanks. I guess I could maybe accept that. But it does seem wierd to me then that I can still check whether I’m allowed to call the function by just trying and catching the MethodError should it occur. It kind of seems like the fundamental thing I’m trying to do is still allowed, just in an uglier way.

Anyway, you don’t have to answer this, but I am slightly curious if theres a simple example of how it would break Julia’s compilation model if there existed a `get_current_world_age` function which was usable from a generated function, and/or if `hasmethod` respected the current world age.

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [June 15, 2021, 10:57pm UTC](https://discourse.julialang.org/t/how-to-get-the-current-world-age-inside-a-generated-function/62969/6 "2021-06-15T22:57:37Z")

</div>

You might want to take a look at what [GitHub - oxinabox/Tricks.jl: Cunning tricks though the julia compiler internals](https://github.com/oxinabox/Tricks.jl) does. It manually attaches MethodInstances as backedges to CodeInfo objects, so that the generated function gets recompiled every time that method could potentially have been redefined. Be warned that this is very much an internal API though.
