Re: Nested Packages and scoping
- To: mathgroup at smc.vnet.net
- Subject: [mg63829] Re: Nested Packages and scoping
- From: dh <dh at metrohm.ch>
- Date: Tue, 17 Jan 2006 04:33:24 -0500 (EST)
- References: <dqack8$1b9$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Detlef, your problem is that you must understand what BeginPackage does. After BeginPackages only System and your package is on the context-path (read the manual. E.g. If you say: g::usage = "g is 5"; then g is created in the Global context. If subsequently you say: BeginPackage["X`"]; g := 5; then you create a new G in the X-context. Here is an example with 3 netsed contexts: BeginPackage["A`"]; f1::usage = "f1 ..."; Begin["`Private`"]; f1 := Print["in package A"]; End[]; EndPackage[]; BeginPackage["B`", "A`"]; f2::usage = "f2 ..."; Begin["`Private`"]; f2 := (Print["Calling A from B:"]; f1); End[]; EndPackage[]; BeginPackage["C`", "B`"]; f3::usage = "f3 ..."; Begin["`Private`"]; f3 := (Print["Calling C from B:"]; f2); End[]; EndPackage[]; if you now say: f3 you get: Calling C from B: Calling A from B: in package A have fun, Daniel Detlef Müller wrote: > Hello, > > I wrote two Packages, the one, say "A" containing Basics, > and one, say "B" containing higher Functions, using those > of "A". > > Now the User should type > > Needs["B`"] > > and have all the Functions of A and B. > I figured out, that for this following looks > awkward, but seems the only way: > > --------------------- > > File A.m > > BeginPackage["A`"]; > f::usage="f is 7"; > Begin["`Private`"]; > f:=7; > End[] > EndPackage[] > > --------------------- > > File B3.m: > > Needs["A`"]; (* without this, f not known in calling context *) > > BeginPackage["B3`"]; > > Needs["A`"]; (* without this, f not known in this context *) > Print[f]; > EndPackage[] > > ---------------------- > > But then I had to nest it one Step further. And now a > problem occures: > > If I have a package X.m with > > g::usage = "g is 5"; > BeginPackage["X`"]; > g := 5; > EndPackage[] > > And A.m is switched to > > ------------------------- > > Needs["X`"]; > BeginPackage["A`"]; > Needs["X`"]; > f::usage = "f is 7"; > Begin["`Private`"]; > f := 7; > End[] > EndPackage[] > > ------------------------- > > The following happens: > > Needs["B3`"] > > (g::"shdw"\),Symbol g appears in multiple contexts (X`,Global`) > definitions in context (X`) may shadow or be shadowed by other \ > definitions. mehr... > > {f, g} > {7, g} > > Is there a Way to achieve, that the Symbols of all used > Packages are inherited? > > (Version is Mathematica 5.1) > > Greetings, > Detlef >