Another world age problem

I’m suffering from a world age problem, that Base.invokelatest does not solve. A MWE demonstrating the problem is as follows:

module MyModule
export myfun
myfun(::Type{Int}) = 3

@generated function genfun(::Type{T}) where T
    return Base.invokelatest(myfun, T)
end
end

import .MyModule
MyModule.myfun(::Type{Float64}) = 4
MyModule.genfun(Float64)

This outputs:

ERROR: MethodError: no method matching myfun(::Type{Float64})
The applicable method may be too new: running in world age 33480, while current world is 33481.
Closest candidates are:
myfun(::Type{Float64}) (method too new to be called from this world context.)
@ Main ~/.julia/dev/scratch.jl:11

Does anybody have any ideas as to if and how this can be fixed?

I think the problem is unrelated to world age. The quoted function defines myfun for an Int parameter, whereas you want to define it for a Type{Int} parameter.

1 Like

Thanks for spotting the typo in my MWE. I’ve corrected this. You can see that it doesn’t fix the problem.

There is a helpful discussion on this particular problem here.

Instead of:

This works (Julia1.9 here):

@generated function genfun(t)
       return :(Base.invokelatest(myfun, t))
end

(as a general rule, generated functions should return a code expression… but there are bigger generated-function experts around)

1 Like

Thanks. Yes, I’m aware of this solution. However, the point of generated functions is to do something at compile time. Your fix doesn’t - it is no better than using a normal function. What I’m trying to do is exploit some potential compile time optimizations. However, the world age problem makes it not possible.

This PR sounds like a fix has been merged into julia master branch. However, it looks like it’s not in v1.9.1, and I guess because it’s a change in behaviour, it won’t be released until v1.10. :slightly_frowning_face: