Package extensions are typically used when the user of a package may or may not already have a certain functionality-provided package installed in his own environment.
But how can I use them instead as a conditional dependency of a certain package, depending on data ?
Let me explain:
I have a model GenFSM
where at a certain point it needs to get the functionality of another package that is specified in the data (in the real application, this is an initialization of forest resources for the region specified in the settings).
So, I thought to use a Package extension for this (these) region-specific packages.
My code is as follows;
src/GenFSM.jl
module GenFSM
export run
function run()
resource_init_regions = ["fr"] # loaded from scenario specific data
for reg in resource_init_regions
toimport = "GenFSM_resource_init_$reg" # name of the package dealing with the initialization for this region
expr = Meta.parse("import $toimport")
eval(expr)
tocall = "resource_init_$reg"
getfield(GenFSM, Symbol(tocall))()
end
end
end # module GenFSM
ext/ResourceInitFrExt.jl:
"""
ResourceInitFrExt
Extension module for initilising the Resource module for France
"""
module ResourceInitFrExt
using GenFSM, GenFSM_resource_init_fr
function GenFSM.resource_init_fr()
# Do the initialization by calling the init function in the package GenFSM_resources_fr
println("hello")
end
end # module
GenFSM Project.toml:
[weakdeps]
GenFSM_resources_init_fr = "78598d3f-d65b-4630-9136-a3bfe5c9de2f"
[extensions]
ResourceInitFrExt = "GenFSM_resources_init_fr"
My questions:
a. Is this approach idiomatic, or there is a better approach to selectively load and use a package that is data-dependant ?
b. Running the code above, I have the error:
ERROR: ArgumentError: Package GenFSM does not have GenFSM_resource_init_fr in its dependencies:
- You may have a partially installed environment. Try `Pkg.instantiate()`
to ensure all packages in the environment are installed.
- Or, if you have GenFSM checked out for development and have
added GenFSM_resource_init_fr as a dependency but haven't updated your primary
environment's manifest file, try `Pkg.resolve()`.
- Otherwise you may need to report an issue with GenFSM
How can I add the (unregistered) package used in the extension to GenFSM
?
If I add it with add
it will be installed as a “normal” dependency…