I’m working on a metapackage (HermitianDiagonalization.jl) that implements a bunch of methods based on the functionality of some third-party packages (Arpack, ArnoldiMethod, etc…), provided they have been loaded by the user. I’m using Requires.jl for this. There is one bit of functionality I’m struggling to get working.
I want to write a function diagonalizer(matrix, packagename::Module) that does different things on a matrix depending on packagename of a loaded package, or throw an error if the package was not loaded. How can I achieve this? (I would also appreciate an answer of the sort “you’re approaching this completely wrong, try this instead”). Thanks!
What I tried
I thought about using Julia’s dispatch machinery. Now, packagename isa Module, such as Arpack. I cannot dispatch on different packagenames, because they are not different types themselves, they are just different objects of type Module. I could just try to use an if-elseif construct, such as
if packagename == Arnoldi
...
elseif packagename == KrylovKit
...
...
end
But of course this doesn’t work, because the symbols Arnoldi, KrylovKit etc need not be defined in any given session (if the user didn’t load them). I also tried building a defaultmethods = Dict{Module,Function} as part of the Requires machinery, but if the module is loaded after loading my package, it does not trigger push! of the new module into defaultmethods, so no dice… I cannot seem to figure this one out!