Making 1.9 weak dependency work

I try update from @require to julia 1.9 weak dependency by following the Pkg documentation at a branch of the DistributionFits package.
It works with Require in Julia 1.8.4, but I do not get it to work with Julia 1.9.0-rc2.
I do not want DistributionFits to depend on Optim (dependency inversion: dependencies should refer only to stable abstractions, not to volatile concretions). If Optim is available, I define a specific AbstractDistributionFitOptimizer and configure DistributionFits to use it.

I do using

using Pkg: Pkg
Pkg.activate(; temp=true)
Pkg.add("Optim")
Pkg.develop("DistributionFits")
using Optim, DistributionFits

How can I acccess the extension module, DistributionFitsOptimExt? It is not available at the global namespace nor as a name inside DistributionFits (as with Require). Furthermore, the command DistributionFits.set_optimizer(OptimOptimizer()) inside the extension module seems to not get executed, because the DistributionFits.df_optimizer is still a NotSetOptimizer.

What did I miss?

Base.get_extension(DistributionFits, :DistributionFitsOptimExt)

I think it may need to go in the extension module’s __init__ function due to how precompilation works.

1 Like

Thanks @marius311
I discovered: Base.get_extension(DistributionFits, :DistributionFitsOptimExt) (get rather than load, which resulted in errors):

DistributionFitsOptimExt = isdefined(Base, :get_extension) ? Base.get_extension(DistributionFits, :DistributionFitsOptimExt) : DistributionFits.DistributionFitsOptimExt

The second option works with Require.
Putting the initialization code to __init __ of the extension package solved the problem, without obtaining an explicit reference to the module.

1 Like

My bad, youre right its get_extension (edited to reflect so not to confuse any future people)