4

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. :)

HyperGroups
  • 8,619
  • 1
  • 26
  • 63
  • Duplicate: http://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
  • @AlbertRetey Fair enough :) – rm -rf May 22 '13 at 15:18

1 Answers1

3

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[]
Albert Retey
  • 23,585
  • 60
  • 104
  • 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 precedingPrivatecreates the context ``MyTest1Private``, and 2. sinceBeginPackageonly adds ``MyTest1 to the `$ContextPath`, the symbols defined inMyTest1Private `` 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
  • @rcollyer: I made it a community wiki, so pleas add as much as you want... – Albert Retey May 22 '13 at 14:22
  • 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
  • haha, I found the reason why I delete Privatefrom the packages template,and without use Private for several months. For my lazy, just now, I Found that: If I didn't add usages, I'll not be able to use Command-Completion for the function. – HyperGroups May 22 '13 at 15:00
  • @LeonidShifrin: please feel free you excellent points to the answer... – Albert Retey May 22 '13 at 15:15
  • @AlbertRetey I forgot to say thanks yesterday. :) – HyperGroups May 23 '13 at 12:42
  • @rcollyer I forgot to say thanks yesterday. :) – HyperGroups May 23 '13 at 12:42
  • @LeonidShifrin Yesterday, I deeply feel that Private`` is important, :), Before I always use a whole notebook or simple packages without Private to accomplish my tasks.. – HyperGroups May 23 '13 at 12:46
  • @HyperGroups: you are welcome, and it is a nice gesture to explicitly thank. On the other hand -- getting answers to questions is the purpose of the site and reputation is the currency :-) – Albert Retey May 23 '13 at 13:31