0
$\begingroup$

Trying to simulate namespaces, I have created two packages with nearly identical function and variable names, essentially, something like this:

BeginPackage["fruit`"];
fruitMain::usage="something";
Begin["`private`"];
nutritionalValue[f_]:=Module[{},_];
calories[f_]:=Module[{},_];
End[];
fruitMain[]:=Module[{},_];
EndPackage[];

and then, preferably in the same notebook (but a different nb would be OK)

BeginPackage["dairy`"];
dairyMain::usage="something";
Begin["`private`"];
nutritionalValue[f_]:=Module[{},_];
calories[f_]:=Module[{},_];
End[];
dairyMain[]:=Module[{},_];
EndPackage[];

But mathematica does not like this at all, talks about shadowing and being shadowed, syntax coloring indicates undefined variables and functions, etc. In fact, it just doesn't work. What can I do about this? I really only want the functionality of namespaces in C++.

Thanks for any help on this.

$\endgroup$
5
  • 4
    $\begingroup$ Your code doesn't produce any error messages. Please post code that reproduces the problem. $\endgroup$
    – C. E.
    Commented Feb 20, 2018 at 4:42
  • 1
    $\begingroup$ A side comment: all definitions should go into the private section, including dairyMain, otherwise you'll end up littering the package context with unwanted symbols. If that Module had an x variable in it, it would be created in the dairy context. Simply mention the symbols to be exported between BeginPackage and Begin. Those not mentioned won't be exported $\endgroup$
    – Szabolcs
    Commented Feb 20, 2018 at 8:57
  • $\begingroup$ The code that produced the errors is much too bulky to post here, and that which I provided was just a contrived example to show the structure. However, I have since come to believe that the problems I was experiencing were due to "stale" definitions created by the editing process, which was proceeding in two separate notebooks. The upshot: my complaint was probably unfounded, and I apologize for burdening you with it, and at the same time thank you, C.E, for your assistance. I consider the case closed, but being a newbie, don't know whether or how I can do this. $\endgroup$ Commented Feb 22, 2018 at 3:07
  • $\begingroup$ Well, I found out what the "real" problem was (duh): I was including an extra apostrophe in: Begin["private"] Of course, it should have been: Begin["private`"] Something to watch for. $\endgroup$ Commented Feb 23, 2018 at 4:10
  • $\begingroup$ ...continued... Something to watch for. It even seems to be confusing this editor. I don't think it's going to display correctly. So, to make a short story long, I had a spurious back-single-quote in front of private in the Begin statement. $\endgroup$ Commented Feb 23, 2018 at 4:17

1 Answer 1

1
$\begingroup$

You will get only warning messages (the shadowing message is only a warning, not an error) if there are symbols in the context fruit and dairy that overlap; putting them into any subcontext (e.g. private) will prevent shadowing. For example, defining a usage message for calories in the nonprivate part of the package will lead to warnings. Even then you can reference to the different symbols with fruit`calories and dairy`calories. Shadowing means that there might be ambiguities if you call calories: You know, it is like a box of chocolates...

Since you put calories into the private context, their full references are fruit`private`calories and dairy`private`calories.

$\endgroup$
1
  • $\begingroup$ I thank you as well, Hans, for your time and assistance. $\endgroup$ Commented Feb 22, 2018 at 3:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.