Package extensions for Julia < 1.9

Wow, that’s a really helpful guide — the merge commit diff is the most readable there: Merge pull request #856 from SciML/weakdeps · SciML/DiffEqBase.jl@c2de920 · GitHub

My quick takeaways:

  • Use both Requires (pre-1.9) and Extensions, using isdefined(Base, :get_extension) to determine which system to use
  • Put the conditional dependencies in [weakdeps] and [extras] and [test] sections
  • Extensions go in ext/ExtensionExt.jl, with a structure like:
    module UnitfulExt
    
    import DiffEqBase
    isdefined(Base, :get_extension) ? (import Unitful) : (import ..Unitful)
    # ... extensions here
    end
    
    (note that Requires plops the dependencies into the parent module, so you use import ..Dependency, whereas the builtin extensions support just makes it happen)
  • Requires support for older versions has this __init__ for the parent package:
    function __init__()
        @static if !isdefined(Base, :get_extension)
            @require Unitful="1986cc42-f94f-5a68-af5c-568840ba703d" begin
                 include("../ext/UnitfulExt.jl")
             end
        end
    end
    
13 Likes