Re: Second argument of BeginPackage, revisited
- To: mathgroup at smc.vnet.net
- Subject: [mg78481] Re: Second argument of BeginPackage, revisited
- From: Albert <awnl at arcor.net>
- Date: Tue, 3 Jul 2007 05:18:50 -0400 (EDT)
- References: <f5td66$36j$1@smc.vnet.net><f5vs13$keg$1@smc.vnet.net> <f62k72$bod$1@smc.vnet.net> <f6ambq$hte$1@smc.vnet.net>
Hi,
> you are correct, BeginPackage["a`",{"b`",..}].. EndPackage[] will put
>
> all the additional contexts "b`".. into $ContextPath. Therefore, all
>
> variables exported by these contexts will be available.
>
> On the other hand, BeginPackage["a`"] Needs["b`"];.. Endpackage[] will
>
> only put "a`" into $ContextPath, therefore, variables from "b`".. are..
I believe that this is not what andrew was complaining in the first
place, his example had a final second call to Needs:
BeginPackage["a`", {"b`"}]
(* code block 1 *)
EndPackage[]
Needs["b`"]
will not always
BeginPackage["a`"]
Needs["b`"]
(* code block 2 *)
EndPackage[]
Needs["b`"]
which he (and I) think indeed should give identical results and does
not. I hesitated to answer because I don't have a "solution" nor a good
explanation, all I can do is back him up that there is something going
on that is either a bug or at least a feature that needs explaining. I
have worked out a "full blown" example of what he refers to that
everyone can try out I hope will convince everyone that there is indeed
something at least not obvious going on:
copy the following package code into a file tst.m anywhere where
mathematica looks for packages:
BeginPackage["tst`","PhysicalConstants`"];
rb=BohrRadius;
EndPackage[];
Then compare the results of the two sessions below:
------ session one: BeginPackage with two arguments --------------------
In[1]:= BeginPackage["tst2`", "tst`"];
In[2]:= rbi = Convert[rb, Inch];
In[3]:= EndPackage[];
In[4]:= rbi
Out[4]= Convert[5.29177*10^-11 Meter, Inch]
In[5]:= Context[Convert]
Out[5]= "tst2`"
------ session two: two explicit calls to needs --------------------
In[1]:= BeginPackage["tst2`"];
In[2]:= Needs["tst`"];
In[3]:= rbi = Convert[rb, Inch];
In[4]:= EndPackage[];
In[5]:= Needs["tst`"];
In[6]:= rbi
Out[6]= 2.08337*10^-9 Inch
In[7]:= Context[Convert]
Out[7]= "Units`"
---------------------------------------------------------------------
whoever believes that the outcome of session one is intended behavior
needs to convince me.
The only other thing that I can contribute is that this is not a new
problem, I have seen this at least with versions 4 and 5 and I can
imagine very well that it has existed before. (if you want to check on
one of these versions with the above example you need to load both
"Miscellaneous`PhysicalConstants`" and "Miscellaneous`Units`" in tst`).
When I just rerun the example in version 4 and 5 I realized that the
behavior of version 4(.2) was even worse: there the final export of
"Miscellaneous`PhysicalConstants`" and "Miscellaneous`Units`" did not
even work with the approach of session 2, but the definition of rbi is
at least done correctly.
And of course there is a workaround (at least for verions >= 5): Don't
use the second argument but explicit calls to Needs. But that is just
saying that the second argument to BeginPackage does not work as it
should :-)
albert