MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Second argument of BeginPackage, revisited

  • To: mathgroup at smc.vnet.net
  • Subject: [mg78321] Re: Second argument of BeginPackage, revisited
  • From: dh <dh at metrohm.ch>
  • Date: Thu, 28 Jun 2007 04:27:19 -0400 (EDT)
  • References: <f5td66$36j$1@smc.vnet.net>


Hi Andrew,

you misunderstood the Package concept. BeginPackage["a`", {"b`"}] will 

place b in the $ContextPath so that its content can be found, but it 

does not put b in Context[], so that new variables are not created in b.

The following may help. Start a new session and enter:

BeginPackage["b`",{"PhysicalConstants`"}]

Print[SpeedOfLight];

Print[Context[]];

Print[$ContextPath];

EndPackage[]

hope this helps, Daniel





Andrew Moylan wrote:

> Unfortunately (in my opinion), BeginPackage["a`", {"b`"}] does not *quite*

> call Needs[] on its second argument(s).

>  

> Suppose b.m looks like this:

>  

> BeginPackage["b`", {"PhysicalConstants`"}]

> (* some code *)

> EndPackage[]

>  

> Then calling Needs["b`"] in a new kernel results in "PhysicalConstants`"

> being on the list of contexts, so that e.g. SpeedOfLight works as expected.

>  

> But calling BeginPackage["a`", {"b`"}] instead does *not* put

> PhysicalConstants on the list of contexts during the definition of the

> package, so that e.g. SpeedOfLight results in the creation of a new, useless

> symbol called a`SpeedOfLight.

>  

> Finally, for maximum confusion (in my opinion), once EndPackage[] is called

> to finish loading the "a`" package, "PhysicalConstants`" *is* placed on the

> list of contexts, causing shadowing between a`SpeedOfLight and

> PhysicalConstants`SpeedOfLight.

>  

> Effectively, BeginPackage appears to

> 1. Call Needs[] on its second arguments, then

> 2. Temporarily remove from the context path any extra contexts those

> packages added to the context path, then finally

> 3. Put all those temporarily removed packages back onto the context path

> once EndPackage[] is called.

>  

> Can anyone help me understand why this behaviour is useful?

> 

> 




  • Prev by Date: Re: Re: Book or Manual for 6?
  • Next by Date: Problems with DrawGraphics and Mathematica 6.0?
  • Previous by thread: Second argument of BeginPackage, revisited
  • Next by thread: Re: Second argument of BeginPackage, revisited