# IRTools/Cassette question: Is it possible to replace everything with invokelatest?

**URL:** https://discourse.julialang.org/t/irtools-cassette-question-is-it-possible-to-replace-everything-with-invokelatest/94785
**Category:** General Usage
**Tags:** question
**Created:** [February 17, 2023, 10:35am UTC](https://discourse.julialang.org/t/irtools-cassette-question-is-it-possible-to-replace-everything-with-invokelatest/94785 "2023-02-17T10:35:35Z")
**Posts on this page:** 5
**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: [February 17, 2023, 10:35am UTC](https://discourse.julialang.org/t/irtools-cassette-question-is-it-possible-to-replace-everything-with-invokelatest/94785/1 "2023-02-17T10:35:35Z")

</div>

Hi there,  
I would like to build a rewriting system which ensures that everything will call the latest methods. This could be useful for developing long running applications like web servers.

goal

```julia
allinvokelatest() do
  webserver()
end

```

This should replace it to `invokelatest(webserver)`, as well as changing the **latest** webserver definition (lowered code) to also call invokelatest everywhere such that their latest definition also gets rewritten recursively.

Is this possible?

---

<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: [February 17, 2023, 10:50am UTC](https://discourse.julialang.org/t/irtools-cassette-question-is-it-possible-to-replace-everything-with-invokelatest/94785/2 "2023-02-17T10:50:57Z")

</div>

Why does this need to be recursive? If you call `invokelatest(webserver)`, any code called as part of `webserver` should have access to the latest methods up to that point, so there’s no need for a recursive transformation.

The only issue I could imagine would be if `webserver` itself defined new methods, but even in that case replacing any call with `invokelatest` seems like overkill, since the transformation does not need to be recursive beyond that point.

---

<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: [February 17, 2023, 12:53pm UTC](https://discourse.julialang.org/t/irtools-cassette-question-is-it-possible-to-replace-everything-with-invokelatest/94785/3 "2023-02-17T12:53:47Z")

</div>

This is unfortunately not true. By now I understood why:

if you do `invokelatest(webserver)`, the maximum world age will be fixed to the current world age.  
If you then adapt some code, a newer world age is created, however everything within the invokelatest won’t see it, because its maximum is now lower.

Unfortunately, even `Base.invoke_in_world` does not work, because it ignores worlds which are larger than the current world.

It would be great if we somehow could define our own invokelatest, but change the current world to `typemax(UInt)`

> <https://github.com/JuliaLang/julia/blob/00b0a7b7b13daa6795391721918d908b8c975744/src/builtins.c#L772-L781>

```plaintext
JL_CALLABLE(jl_f__call_latest)
{
    jl_task_t *ct = jl_current_task;
    size_t last_age = ct->world_age;
    if (!ct->ptls->in_pure_callback)
        # change this to `typemax(UInt)`
        ct->world_age = jl_atomic_load_acquire(&jl_world_counter);
    jl_value_t *ret = jl_apply(args, nargs);
    ct->world_age = last_age;
    return ret;
}

```

but this is C code - how can we write something like this in Julia?

---

<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: [February 17, 2023, 1:38pm UTC](https://discourse.julialang.org/t/irtools-cassette-question-is-it-possible-to-replace-everything-with-invokelatest/94785/4 "2023-02-17T13:38:00Z")

</div>

Ah, so `webserver` spawns some background task and you’re defining new methods while that task is still running? What kind of methods are supposed to be redefined? For example, if you are listening for http requests and you have a user-defined callback that may be redefined, you don’t need `invokelatest` everywhere, you just invoke the callback with `invokelatest`.

Messing with internals here is unlikely to be very fruitful, since the current semantics are are not without reason. Otherwise, the compiler might get into an inconsistent state resulting in miscompilation.

---

<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: [February 17, 2023, 1:41pm UTC](https://discourse.julialang.org/t/irtools-cassette-question-is-it-possible-to-replace-everything-with-invokelatest/94785/5 "2023-02-17T13:41:05Z")

</div>

thank you for the feedback. Okay, so no `typemax(UInt)` ? ~~I guess, I will just open a suggestion on Julia github to get further feedback~~ [here the issue](https://github.com/JuliaLang/julia/issues/48705)

Meanwhile I build the IRTools implementation. It has some difficulties with distributed/@async, threads and such (all of which should just be special cases of rewrites), but otherwise it works 🙂
