Extensions, world age and generated function

Hello everyone,

I’m writing a package with extensions and ran into a world age issue. The problem is the same as in JuliaLang/julia#19942 and related to this discourse thread.

Minimal example

function bar end

@generated function foo(::Val{x}) where x
    u = bar(x)
    return :($u)
end

bar(x::Integer) = x + 1

Let say foo is defined in the main package, and bar(x::Integer) is defined in an extension triggered by a dependency. This means that when a user does:

using MainPackage
using PackageThatTriggersExtension

the world age problem occurs at load time: the @generated body calls bar at specialization time, but the extension’s method is “too new.”

  1. Is there a way to “finalize” extensions — i.e. run some code in the main package (that is the same for all extensions) each time an extension is loaded? In this case, that hook could force re-specialization of foo.

  2. Alternatively, is there a way to disable precompilation of a specific @generated function, so that it specializes lazily at first call (by which time all extensions are loaded)?

For now, my workaround is to define bar(::SomeTypeRelatedToExt, x::Integer) in each extension. There are other workarounds too, but I’d like to know if there’s a clean way to do this in Julia.