1

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
desertnaut
  • 57,590
  • 26
  • 140
  • 166
PatrickT
  • 10,037
  • 9
  • 76
  • 111

1 Answers1

1

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
Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107
  • Thanks Bogumił! How about reloading a module after edits? No problem with some globals left behind? Typically this need arises when testing what happens if a method uses `Test1.some_method()` vs `Test2.some_method()`. A bit tedious to type. In some cases it will be easier to restart Julia. If I could `import as` (there was talk of introducing that feature, I wonder what happened), I could just do `A.some_method()` and `B.some_method()` and avoid the modules while still having to prefix. – PatrickT May 27 '21 at 08:42
  • Actually, you can do "import aliasing" in the simplest way. `A = PackageOne`, and `B = PackageTwo`. See: https://stackoverflow.com/questions/42104130/module-aliasing-in-julia (not great if you want to use variables `A` and `B` for some other purpose of course) – PatrickT May 27 '21 at 08:55
  • For reloading module after edits use Revise.jl. Import aliasing is a different thing. I thought you have a code you cannot modify that calls `some_method` function and you get ambiguity. – Bogumił Kamiński May 27 '21 at 09:09
  • Thanks. I haven't used `Revise.jl`. Will do. There are several use cases. Say you have a method that goes through several generations as you update a package. At some point you want to compare their action. You write a script where you pull them one by one and have the results within the same script. So my first idea was to switch the `using` or `import` on and off as the need arises. But it seems like it'll be easier to create a list of the methods and then pull them in a loop: then only their position in the list matters, not their actual name. Can you create modules in a loop easily? – PatrickT May 27 '21 at 11:03
  • I will add another approach that fits looping well. – Bogumił Kamiński May 27 '21 at 13:15
  • Your edit looks great. I really like this second approach. Thank you so much! :-) – PatrickT May 27 '21 at 13:35