MathGroup Archive 2003

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

Search the Archive

RE: Namespaces/Context in Packages?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg44827] RE: [mg44791] Namespaces/Context in Packages?
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Wed, 3 Dec 2003 04:24:08 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

>-----Original Message-----
>From: Kastens at Hamburg.BAW.DE [mailto:Kastens at Hamburg.BAW.DE]
To: mathgroup at smc.vnet.net
>Sent: Thursday, November 27, 2003 5:38 PM
>To: mathgroup at smc.vnet.net
>Subject: [mg44827] [mg44791] Namespaces/Context in Packages?
>
>
>Hi!
>
>I'd like to use functions from other packages in my own 
>package. How to do this?
>
>I've tried:
>
>BeginPackage["TestPackage`",{"Statistics`ContinuousDistributions`"}]
>
>TestFunction::usage = "TestFunction[<list>]"
>
>Begin["`Private`"];
>TestFunction[list_]:=
>{
>	Mean[list]
>}
>End[];
>EndPackage[]
>
>The problem is, that Mean is not known in the Testfunction or 
>- if the testfunction is declared outside private - I got the 
>error-msg that Mean is shadowed by other packages.
>Trying to use Statistics`ContinuousDistributions`Mean[list] 
>doesn't work either.
>
>Any suggestions?
>
>Thanks,
>marko
>

If you observe the sequence of commands...


In[1]:= BeginPackage["TestPackage`",
{"Statistics`ContinuousDistributions`"}]
Out[1]= "TestPackage`"

In[2]:= TestFunction::usage = "TestFunction[<list>]"
Out[2]= "TestFunction[<list>]"

In[3]:= Begin["`Private`"];
In[4]:= TestFunction[list_] := {Mean[list]}
        $Context
Out[5]= "TestPackage`Private`"

In[6]:= $ContextPath
Out[6]= {"TestPackage`", "Statistics`ContinuousDistributions`", "System`"}

In[7]:= End[]
Out[7]= "TestPackage`Private`"
In[8]:= EndPackage[]

In[9]:= Names["*`Mean"]
Out[9]= {"Mean", "TestPackage`Private`Mean"}

In[10]:= TestFunction[{2, 3}]
Out[10]= {TestPackage`Private`Mean[{2, 3}]}

In[11]:=  Quit[]

You'll see that other packages Statistics`ContinuousDistributions` depends
upon have not been loaded, and hence not Mean, which is defined in
Statistics`DescriptiveStatistics` (although you are lead to
ContinuousDistributions with Help). So include that too.



If you load ContinuousDistributions` directly, further packages are loaded.
I'd simple include them all in the needs list with BeginPackage to avoid any
surprises.
 
In[1]:= $ContextPath
Out[1]= {"Global`", "System`"}

In[2]:= << Statistics`ContinuousDistributions`

In[3]:= Names["*`Mean"]
Out[3]= {"Mean"}

In[4]:= $ContextPath
Out[4]= {"Statistics`ContinuousDistributions`",
"Statistics`NormalDistribution`", \
"Statistics`Common`DistributionsCommon`", \
"Statistics`DescriptiveStatistics`", "Statistics`DataManipulation`", \
"DiscreteMath`Tree`", "Global`", "System`"}

In[5]:= Quit[]


--
Hartmut


  • Prev by Date: Re: Bad Command or FIlename (v2.2.2DOS)
  • Next by Date: Re: Disabling Groups of Cells
  • Previous by thread: RE: Namespaces/Context in Packages?
  • Next by thread: Parametric 2d plot with both axes logarithmic