juliajulia-pkg

How to detach a package without restarting


Ref: docs for Pkg

I want to compare two methods with the same name from two different packages in the same session/script. To avoid name collision, I would like to negate using (i.e. "undo" / "reverse"). Something like what detach does for R.

using PackageOne
some_method()

undo using PackageOne  # <-- negate `using PackageOne` without restarting
using PackageTwo
some_method()  # name collision avoided here

Solution

  • You cannot detach a package that is already loaded in some module AFAICT. What you can do is wrap your code using these methods in a module like this:

    module Test1
        using PackageOne
        some_method()
    end
    
    module Test2
        using PackageTwo
        some_method()
    end
    

    another approach would be:

    using PackageOne
    using PackageTwo
    
    methods = [PackageOne.some_method, PackageTwo.some_method]
    
    function test(some_method)
        # here use some_method
    end
    
    for method in methods
        test(method)
    end