# World Age Problem Explanation

**URL:** https://discourse.julialang.org/t/world-age-problem-explanation/9714
**Category:** General Usage
**Tags:** world\_age
**Created:** [March 14, 2018, 12:33pm UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714 "2018-03-14T12:33:07Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![LS64](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ls64/32/3743_2.png) [@LS64](https://discourse.julialang.org/u/LS64)
#### Post date: [March 14, 2018, 12:33pm UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/1 "2018-03-14T12:33:07Z")

</div>

Hello.

Recently I have faced the “world age problem” several times. That means, after searching for my problems, this website came up with themes like “world age workaround” or “How to Bypass the World Age Problem”.  
I am not a computer scientist, so maybe this is a stupid question, but I have not found a explanation of this problem; where can I get information what actually happens there? Or, more clearly, what is the “World Age Problem”?  
As you can imagine, if I google this, I get a lot of information about our Earth’s age…

I am not absolutely sure, if this topic fits the “Usage” category. So if I should have decided another part of this board, I apologize for this.

Thank you in advance.  
LS64

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [March 14, 2018, 1:54pm UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/2 "2018-03-14T13:54:03Z")

</div>

[How to Bypass the World Age Problem](https://discourse.julialang.org/t/how-to-bypass-the-world-age-problem/7012) does have some information  
Seems to be to do with redeclaring functions and/or the order they are defined in, and using eval / @eval

---

<div class="post-metadata">

### Author: ![LS64](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ls64/32/3743_2.png) [@LS64](https://discourse.julialang.org/u/LS64)
#### Post date: [March 15, 2018, 11:15am UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/3 "2018-03-15T11:15:19Z")

</div>

Thank you for this information. I’ve read some pages and maybe, I get an idea.

But I need a starting point to understand this issue completely and want to be sure, that I’ve understood this correctly. Can someone please revise or confirm the following?

Every compilation of a function gets a “time stamp”, here known as world age. If I change dependencies to already compiled functions in a new world and execute my code, I get these errors? (In my case, these functions are parts of packages (e.g. Plots), which heightens the level of complexity to me.)  
So, in the end, it is precompilation/recompilation problem, which I can solve by using eval, because @eval forces a new compilation in the latest world (by letting jl\_toplevel\_eval\_flex() decide again, what to do)?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [March 15, 2018, 11:29am UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/4 "2018-03-15T11:29:03Z")

</div>

The reason for worlds is because of optimizations. If functions can be defined (and redefined) at arbitrary points (and instantly take effect), then that would limit the possible optimizations that can be made. Therefore, new functions are only made visible to the system (become usable) after hitting toplevel. If you try to call a new function before this, then you get a world error.

In some cases (like if you are writing a REPL), you do actually want to call your newly defined function without hitting toplevel. This can be done by using `Base.invokelatest(f, args...)` but it will of course prevent certain optimizations in the scope where `f` is called (it can pretty much not be reasoned about at all, so no inlining or inference).

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [March 15, 2018, 1:26pm UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/5 "2018-03-15T13:26:21Z")

</div>

Check out ["running in world age X, while current world is Y" errors](https://discourse.julialang.org/t/running-in-world-age-x-while-current-world-is-y-errors/5871) for another explanation.

---

<div class="post-metadata">

### Author: ![shorty66](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shorty66/32/3254_2.png) [@shorty66](https://discourse.julialang.org/u/shorty66)
#### Post date: [March 15, 2018, 2:35pm UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/6 "2018-03-15T14:35:08Z")

</div>

You might also want to share a minimal working example (MWE) of your code. In my experience, world age issues are rather uncommon and perhaps you are using `eval` in cases where you shouldn’t.

---

<div class="post-metadata">

### Author: ![LS64](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ls64/32/3743_2.png) [@LS64](https://discourse.julialang.org/u/LS64)
#### Post date: [March 16, 2018, 10:40am UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/7 "2018-03-16T10:40:12Z")

</div>

Thank you very much for your answer, kristoffer.carlsson. This helps me a lot.

I will take a closer look at this whole theme on the weekend; especially at your link, mbauman.

If errors related to world age continue to appear, I will let you know and share my code. At the moment, I just want to understand the point in general.

Thank you all!

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [March 16, 2018, 7:21pm UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/8 "2018-03-16T19:21:01Z")

</div>

I would add that requiring modifications to dispatch to take effect immediately is one of those classic behaviors in dynamic languages that makes them hard/impossible to compile without heroic efforts (along with reified local scopes, the ability to add state to objects dynamically at any point, etc. – we should make a list some time). In Julia we’re nipping that one in the bud by defining the language to work in a way that can be implemented in the presence of compilation without insane contortions, namely with world ages that only take effect at the top-level, just the way that @kristoffer.carlsson described.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 16, 2018, 8:50pm UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/9 "2018-03-16T20:50:22Z")

</div>

Seel also [here for a detailed explanation](https://discourse.julialang.org/t/help-calling-a-function-defined-from-expressions/1340/4), and [here for an explanation of invokelatest](https://discourse.julialang.org/t/is-there-a-way-to-manually-trigger-a-world-age-update/7776/2).

It would be nice to have something about this in the manual at some point…

---

<div class="post-metadata">

### Author: ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)
#### Post date: [December 3, 2019, 10:49am UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/10 "2019-12-03T10:49:47Z")

</div>

This is a great explanation, thank you! Would you be so kind as to elaborate on “after hitting toplevel” please? What does that mean? Thanks

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [December 3, 2019, 2:09pm UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/11 "2019-12-03T14:09:59Z")

</div>

Top level is where the execution hits global scope for example if the code below was a file that we run, I’ve indicated the places where “top level” is reached

```julia
<-------------- top level
module M
<-------------- top level
function f(x)
     return x
end
<-------------- top level
struct Bar
    a::Int 
end
<-------------- top level
f(Bar(3))
<-------------- top level
end
<-------------- top level

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [December 3, 2019, 5:04pm UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/12 "2019-12-03T17:04:44Z")

</div>

While the term is familiar to users who used some kind of Lisp or similar, I wonder if the manual should define/explain _toplevel_, as users coming from Matlab/R/Python may not have seen it in this context.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [November 5, 2020, 12:57am UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/13 "2020-11-05T00:57:05Z")

</div>

There is now a very good and readable research paper about Julia’s novel world age mechanism as a way to avoid needing on-stack replacement in the presence of `eval`:

> **[World Age in Julia: Optimizing Method Dispatch in the Presence of Eval...](https://arxiv.org/abs/2010.07516)**
>
> Dynamic programming languages face semantic and performance challenges in the presence of features, such as eval, that can inject new code into a running program. The Julia programming language introduces the novel concept of world age to insulate...

I think one of the most important observations about this is that in the presence of concurrency in a modern compiled system, the classic notion that `eval` takes immediate effect is actually not well-defined and therefore quite confusing. Saying instead that changes don’t kick in until the next top-level evaluation simplifies the mental model considerably and is actually well-defined. Here’s a twitter thread about the paper: [https://twitter.com/julbinb/status/1317195401846554624](https://twitter.com/julbinb/status/1317195401846554624).

---

<div class="post-metadata">

### Author: ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)
#### Post date: [November 5, 2020, 7:25am UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/14 "2020-11-05T07:25:17Z")

</div>

A minimal example showing this effect:

```julia
foo() = "foo"
function makebar()
	sleep(rand())
	res= (foo(), Base.invokelatest(foo))
	@eval foo() = "bar"
	res
end
fetch.(Threads.@spawn makebar() for i in 1:2)

```

The standard call to `foo()` always returns “foo” for both threads, `Base.invokelatest(foo)` returns “foo” for the thread with the shorter sleep time and “bar” for the other one.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [November 5, 2020, 7:52pm UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/15 "2020-11-05T19:52:02Z")

</div>

If you want to just demonstrate the basic world age behavior, you don’t need threads or anything, you just evaluate a new function definition in the same local scope before calling it:

```julia
f() = 1
function g()
    @eval f() = 2
    f()
end

```

If you call `g` once it will return 1, if you call it again it will return 2. If you do this instead:

```julia
f() = 1
function g()
    @eval f() = 2
    Base.invokelatest(f)
end 

```

This will return 2 every time you call it.

---

<div class="post-metadata">

### Author: ![atteson](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@atteson](https://discourse.julialang.org/u/atteson)
#### Post date: [June 24, 2023, 2:13pm UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/16 "2023-06-24T14:13:04Z")

</div>

Does anyone have a recommendation on how to make the following minimal example work?

```julia
function g( i )
    f = eval( :( () -> $i ) )
    f()
end

g(1)
g(2)

```

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [June 24, 2023, 5:24pm UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/17 "2023-06-24T17:24:33Z")

</div>

Given the completely unspecified requirements of what you want to do the only suggestion I can give you is:

```julia
julia> g(i) = i
g (generic function with 1 method)

julia> g(1)
1

julia> g(2)
2

```

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [June 28, 2023, 2:05pm UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/18 "2023-06-28T14:05:10Z")

</div>

Ideally don’t use eval at all:

```julia
julia> function g(i)
           f = () -> i
           f()
       end
g (generic function with 1 method)

julia> g(1)
1

julia> g(2)
2

```

If you must use eval (why?) then use `invokelatest` to call `f`:

```julia
function g(i)
    f = eval(:(() -> $i))
    invokelatest(f)
end

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [June 28, 2023, 3:25pm UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/19 "2023-06-28T15:25:14Z")

</div>

Use RuntimeGeneratedFunctions.jl.

```julia
using RuntimeGeneratedFunctions
RuntimeGeneratedFunctions.init(@ __MODULE__ )

function g( i )
    f = @RuntimeGeneratedFunction( :( (x) -> $i ) )
    f(1)
end

g(1)
g(2)

```

It doesn’t handle no argument functions ([Failure when defining a function with no args · Issue #53 · SciML/RuntimeGeneratedFunctions.jl · GitHub](https://github.com/SciML/RuntimeGeneratedFunctions.jl/issues/53)) but that’s easy to fix for anyone who specifically needs that.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [June 29, 2023, 2:56pm UTC](https://discourse.julialang.org/t/world-age-problem-explanation/9714/20 "2023-06-29T14:56:19Z")

</div>

That works, but in this example at least, there’s no need for eval at all, you can just use a closure. If there is some need for eval to construct the body of the inner function based on values, then generated functions are indeed the way to go.

[Next page](https://discourse.julialang.org/t/world-age-problem-explanation/9714.md?page=2)
