# Cross referencing symbols in different packages [duplicate]

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! Privatefoo[] {"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!
Privatebar[]
{"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 Privatefoo[]? It is never defined.

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

• 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. – Leonid Shifrin Jun 13 '15 at 17:02
• 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? – Johu Jun 13 '15 at 17:24
• 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. – Leonid Shifrin Jun 13 '15 at 17:27