Let us suppose we have two packages: A and B.
BeginPackage["A`"]
Foo::usage="bla bla";
Begin["`Private`"]
Foo[x_]:= x^2;
SetAttributes[Foo,{Protected,ReadProtected,Locked}];
End[]
EndPackage[]
.
BeginPackage["B`"]
Foo::usage="bla bla";
Begin["`Private`"]
Foo[x_]:= x^3;
SetAttributes[Foo,{Protected,ReadProtected,Locked}];
End[]
EndPackage[]
Obviously, if the user tries to load both packages on the same kernel, there will be shadowing issues. Normally one would just rename one of the functions to avoid that kind of conflicts. However, if both packages come from different developers and are encrypted/proprietary (i.e. there is no way to modify the source code) then the user seems to be out of luck.
So is there any possibility to load the two packages on the same kernel such, that the functions Foo from A and B will not go into the Global context? Then the user could use just A`Foo and B`Foo to distinguish between the two. Or are there possibly other tricks to have both functions in the same session?
Foowill not go in theGlobal`context. If you evaluate the code you posted in a newly started kernel, you'll getA`FooandB`Fooand you can refer to them as such. You will not getGlobal`Foo. Simply typingFoowill refer toB`Foobecause package B was loaded last and thusB`appears first in$ContextPath. – Szabolcs Mar 19 '15 at 21:52Globalhere. My mistake. But I'm mainly worried about the warning messages that Mathematica issues when I load both packages. Can I really ignore them (in this case) without any issues? That would mean that I can essentially doOff[General::shdw];Needs["A"];Needs["B`"];On[General::shdw]` without any problems, right? – vsht Mar 19 '15 at 22:20$ContextPath = Select[$ContextPath, (! StringMatchQ[#, "A`" ~~ ___] && ! StringMatchQ[#, "B`" ~~ ___]) &];I can ensure that there will be no confusion betweenFoofromAandB. Hmm, this seems to be much easier than I thought. – vsht Mar 19 '15 at 22:35StringMatchQ.$ContextPathshould simply contain"A`", notA`followed by something. If you like, just useDeleteCases[$ContextPath, "A`"|"B`"]– Szabolcs Mar 19 '15 at 22:39D,Dot,Timesetc. ) or put their variables explicitly into the global context. In this situation the safest thing (in my view) would be to start those packages on parallel kernels and communicate with them viaParallelEvaluate. Although I'm sure that I'm not the first one who got that idea. – vsht Mar 20 '15 at 17:54