Invoke previous method version with identical signature

Very simple example:
I have a function f with one method, I would like to redefine that method, but call the old implementation from inside the new one. invoke cannot do this for identical signatures. The following leads to the most recent version of f() being called recursively:

function f()
  println("Old version")
end

# Redefining the same method, but with extra behaviour
function f()
  invoke(f)
  println("New version")
end

My desired output would be for each print statement to run once.

I found a suggestion somewhere to use world-age, which almost seems to have the desired effect, but using invoke_in_world prevents the old method from using up-to-date versions of other functions, which is not what I want.

When you redefine a method, you discard the old method (in an obsolete world age), so it fundamentally cannot use updated methods of other functions (in the current world age). Package code disallows direct method replacement like this, too. The easiest alternative is to put the old method body in a new helper function, say _f, that the new method calls. If you’d only call it once, you could instead edit the method directly with the new surrounding code.

1 Like

In what context are you doing this? This is not a normal way to program in Julia, and probably there is another way to accomplish your end goal.