Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

When I'm tiding up my simple packages, shadow awlays comes out. Here is an example.

Packages1

       BeginPackage["MyTest1`"]

       f1[x0_]:=Module[{x=x0},StringReplace["sdfasdfwhat hahahahah",x_->"DummyInMyTest1"]]

       g1[x0_]:=Module[{x=x0},StringReplace["sdfasdfwhat hahahahah",x_->"DummyInMyTest1"]]

       EndPackage[]

Packages2

       BeginPackage["MyTest2`"]

       f2[x0_]:=Module[{x=x0},StringReplace["sdfasdfwhat hahahahah",x_->"DummyInMyTest2"]]
       g2[x0_]:=Module[{x=x0},StringReplace["sdfasdfwhat hahahahah",x_->"DummyInMyTest2"]]

       EndPackage[]

At first time, they are in one packages, now split them into two packages. Shadows about x is confusing, for I've used Module... actually I wish it to prevent the shadow problem but failed. Here we go

`$ContextPath`
Out[2]= {PacletManager`,QuantityUnits`,WebServices`,System`,Global`}
In[3]:= <<MyTest1`
In[4]:= <<MyTest2`
In[4]:= x0::shdw: Symbol x0 appears in multiple contexts {MyTest2`,MyTest1`}; definitions in context MyTest2` may shadow or be shadowed by other definitions. >>
In[4]:= x::shdw: Symbol x appears in multiple contexts {MyTest2`,MyTest1`}; definitions in context MyTest2` may shadow or be shadowed by other definitions. >>
In[4]:= x$::shdw: Symbol x$ appears in multiple contexts {MyTest2`,MyTest1`}; definitions in context MyTest2` may shadow or be shadowed by other definitions. >>
In[5]:= $ContextPath
Out[5]= {MyTest2`,MyTest1`,PacletManager`,QuantityUnits`,WebServices`,System`,Global`}

How to avoid these shadows but Quiet?

Maybe duplicates with some shadows Q/A. :)

share|improve this question
    
Duplicate: mathematica.stackexchange.com/q/5563/5 –  rm -rf May 22 '13 at 13:55
    
@rm-rf: I could well imagine that this question has a duplicate, but I don't think the one you mention is one, woudn't you agree... –  Albert Retey May 22 '13 at 14:11
    
I have to agree with Albert here, this differs in that the OP seems to want to avoid shadowing in the first place, not cover it up. –  rcollyer May 22 '13 at 14:21
    
@AlbertRetey Yes, I agree. I think this one (your question) is a better candidate. This one perhaps briefly talks about exposing only those that are necessary, but it wasn't in the context of shadowing. –  rm -rf May 22 '13 at 14:34
    
@rm-rf: my own I did remember, but don't think this is an (exact) duplicate for either of them. To me it rather seems that the problem might be a candidate for "too localized" as you could argue it's a very unusual (?) "wrong" usage of the package functionality which could be avoided by reading the docs. But that would probably be true for almost every question... –  Albert Retey May 22 '13 at 15:13

1 Answer 1

up vote 3 down vote accepted

When the conflicts you see only affect symbols that you didn't mean to export but only use for internal purposes, then you should take care to not export them. Here is the standard way to do this:

   BeginPackage["MyTest1`"]

   (* mention those symbols you want to export here, standard convention is to   
      define a usage message for them... *)
   f1::usage = "f1[x] calculates something...";
   g1::usage = "g1[x] calculates something else...";

   Begin["`Private`"];

   f1[x0_]:=Module[{x=x0},StringReplace["sdfasdfwhat hahahahah",x_->"DummyInMyTest1"]]

   g1[x0_]:=Module[{x=x0},StringReplace["sdfasdfwhat hahahahah",x_->"DummyInMyTest1"]]

   End[]

   EndPackage[]
share|improve this answer
    
beat me to it. +1 –  rcollyer May 22 '13 at 14:11
    
@rcollyer: question remains: do I type faster or am I better at copy and paste? :-) Thanks anyway... –  Albert Retey May 22 '13 at 14:13
    
Two notes: 1. the ` preceding Private creates the context MyTest1`Private` , and 2. since BeginPackage only adds MyTest1` to the $ContextPath, the symbols defined in MyTest1`Private` need to be completely specified by their contexts to be globally accessible. In other words, no more shadowing. –  rcollyer May 22 '13 at 14:15
    
Faster at copy and paste. I was adding a lot of explanatory text. Oh well, you can add my notes to your answer, if you wish. –  rcollyer May 22 '13 at 14:16
1  
+1. I think, a lot of people don't realize that this is the main purpose for Private` contexts to exist, since otherwise a lot of "local" symbols generated will conflict with each other. In some sense, the role of private contexts is more important than what can be seen on the surface (which is, only localize private package-scoped variables and functions), because in addition to this main role,partially they make up for imperfections (emulation) of lexical scoping in Mathematica. In truly lexically-scoped environments, all those Module-generated and other symbols wouldn't have existed. –  Leonid Shifrin May 22 '13 at 14:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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