Re: Nested Packages and scoping
- To: mathgroup at smc.vnet.net
- Subject: [mg63836] Re: Nested Packages and scoping
- From: albert <awnl at arcor.de>
- Date: Tue, 17 Jan 2006 04:33:30 -0500 (EST)
- References: <dqack8$1b9$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi, > 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[] > >---------------------- No, and it's not even the recommended way to handle this situation: you can give an optional second argument to BeginPackage that will do exactly what you want: BeginPackage["B`",{"A`"}] and of course you can make B` depend on more than one package. > 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[] > > (g::"shdw"\),Symbol g appears in multiple contexts (X`,Global`) > definitions in context (X`) may shadow or be shadowed by other \ > definitions. mehr... this seems to happen because you defined the usage for g _before_ the BeginPackage and has nothing to do with nesting package references. I believe that even your first approach should work equally well when defining g::usage after BeginPackage. I have written many packages with deeply nested dependences of subpackages and never had problems when using the second argument form of BeginPackage where appropriate. With some care you can even construct cross references (like A needs B and B needs A) without shadow messages. Anyway that is usually not a good idea. hth albert