3
$\begingroup$

This question already has an answer here:

I have problems with name space shadowing which I do not know how to resolve. I have two packages which depend on each other. Here is example of two files

fooPackage.m:

BeginPackage["fooPackage`",{"barPackage`"}];
top::usage = "This is top level funciton";
foo::usage = "Text";
Begin["Private`"];

top[arg___]:=bar[];
foo[arg___]:=Print["This is foo!"];

End[];
EndPackage[];

barPackage.m:

BeginPackage["barPackage`", {"fooPackage`"}];
bar::usage = "";
Begin["Private`"]

bar[arg___] := (Print["This is bar!"]; foo[arg])

End[];
EndPackage[];

Now if I use it as I intended:

Quit[];

Needs["fooPackage`", "/home/johu/temp/fooPackage.m"]

top[]
$ContextPath[[;; 2]]
This is bar!
Private`foo[]
{"fooPackage`", "barPackage`"}

I would get bar working if I loaded another package as top level which however does not contain the definition top

Quit[];

Needs["barPackage`", "/home/johu/temp/barPackage.m"]

bar[b]
top[b]
$ContextPath[[;; 2]]
This is bar!
This is foo!
Private`bar[]
{"barPackage`", "fooPackage`"}

My question is how to avoid this shadowing or what would be the cleanest workaround?

Perhaps you can also explain, why in the first case does it even make a name for local symbol Private`foo[]? It is never defined.

Here are some references to relevant educational sources, which however did not solve my confusion

$\endgroup$

marked as duplicate by Leonid Shifrin, m_goldberg, Michael E2, Community Jun 13 '15 at 18:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ Actually, I just realized that this was asked already before. I will port this version of my answer to that post, which contains a less extensive one. $\endgroup$ – Leonid Shifrin Jun 13 '15 at 17:02
  • $\begingroup$ Yes, the discussion was very relevant. I exactly have the mainPackage & helperPackage issue. Solution 2 from the referred is the most acceptable one. I guess I should accept the duplicate tag then? $\endgroup$ – Johu Jun 13 '15 at 17:24
  • $\begingroup$ Good to know it helped. Re: accept - you don't have to do anything. If you want, you can become one of the closers for this question, by adding your vote to close this one as a duplicate (if you have the right to close questions, I don't remember how much rep it takes for that). If you feel like it, you can vote for that discussion question / answer. One thing though: please don't delete this one - it will serve as another gateway to that discussion. $\endgroup$ – Leonid Shifrin Jun 13 '15 at 17:27