4
$\begingroup$

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

$\endgroup$
6
  • $\begingroup$ Duplicate: mathematica.stackexchange.com/q/5563/5 $\endgroup$
    – rm -rf
    May 22, 2013 at 13:55
  • $\begingroup$ @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... $\endgroup$ May 22, 2013 at 14:11
  • $\begingroup$ 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. $\endgroup$
    – rcollyer
    May 22, 2013 at 14:21
  • $\begingroup$ @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. $\endgroup$
    – rm -rf
    May 22, 2013 at 14:34
  • $\begingroup$ @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... $\endgroup$ May 22, 2013 at 15:13

1 Answer 1

3
$\begingroup$

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[]
$\endgroup$
12
  • $\begingroup$ beat me to it. +1 $\endgroup$
    – rcollyer
    May 22, 2013 at 14:11
  • $\begingroup$ @rcollyer: question remains: do I type faster or am I better at copy and paste? :-) Thanks anyway... $\endgroup$ May 22, 2013 at 14:13
  • $\begingroup$ 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. $\endgroup$
    – rcollyer
    May 22, 2013 at 14:15
  • $\begingroup$ 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. $\endgroup$
    – rcollyer
    May 22, 2013 at 14:16
  • 1
    $\begingroup$ +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. $\endgroup$ May 22, 2013 at 14:29

Your Answer

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

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