How to use functions defined in package extensions but not the main package?

I just started using the package extension functionality introduced after v1.9. I am a bit confused about its usage. Can I ask when the package extension is loaded, where are the new functions (which are not defined in the original package) registered?

For example, there are some new functions in ext/ForwardDiffStaticArraysExt.jl such as dualize. How can I use it if I want to (just an example, not that I really need it)? Here are my attempts:

julia> using ForwardDiff: dualize
ERROR: UndefVarError: `dualize` not defined

julia> using StaticArrays

julia> using ForwardDiff: dualize
ERROR: UndefVarError: `dualize` not defined

julia> using ForwardDiff

julia> names(ForwardDiff)
2-element Vector{Symbol}:
 :DiffResults
 :ForwardDiff

julia> using ForwardDiff.ForwardDiffStaticArraysExt
ERROR: UndefVarError: `ForwardDiffStaticArraysExt` not defined

julia> using ForwardDiffStaticArraysExt
ERROR: ArgumentError: Package ForwardDiffStaticArraysExt not found in current path.
- Run `import Pkg; Pkg.add("ForwardDiffStaticArraysExt")` to install the ForwardDiffStaticArraysExt package.
Stacktrace:
 [1] macro expansion
   @ ./loading.jl:1595 [inlined]
 [2] macro expansion
   @ ./lock.jl:267 [inlined]
 [3] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:1576

Are package extensions only used for extending existing methods? Can I define new methods and use them?

Similar to What things i can/cannot do within an extension?

in the extension:

f1(x) = 2*x

in the package:

function test_ext(x)
    ext_sym = :Pkg1Ext #the name of the extension
    ext = Base.get_extension(@__MODULE__(),ext_sym)
    if ext !== nothing
        return ext.f1(x)
    else
        throw_error("extension not loaded!")
    end
end

in your specific case:

dualize(t,x) = Base.get_extension(ForwardDiff,:ForwardDiffStaticArraysExt).dualize(t,x)
2 Likes