Re: Shadow problem of a variable defined in a function context inside different packages
- To: mathgroup at smc.vnet.net
- Subject: [mg117979] Re: Shadow problem of a variable defined in a function context inside different packages
- From: Albert Retey <awnl at gmx-topmail.de>
- Date: Thu, 7 Apr 2011 08:04:37 -0400 (EDT)
Hi, > > I recovered this peace of code here in the forum (sorry but I no > longer remember it's exact origin), that I use at the end of a > package, to automatically set attributes to all the definitions of the > package. > > ToExpression[Names["myPackage`*"], InputForm, Function[x, > SetAttributes[x, {ReadProtected,Locked}], HoldAll]] > > The problem with it is that whenever I load two different packages > that were created using this same piece of code, I get a shadowing > message for symbol x. > > Can't understand why, since x is in the scope of the function. > I tried to do a Module, but with no success. > > I ended up with the following alternative, that works fine: > ToExpression[Names["myPackage`*"], InputForm, SetAttributes[#, > {ReadProtected,Locked}]&] > > Nevertheless I was wondering why the first expression doesn't work. > Any clues? Actually you missed to tell us the most important part: where exactly is that line in your package file and do these packages "Need" each other? From what I see I would guess that you did put that line between End[] and EndPackage[], at least then I would expect what you see, namely an shadowing x for every package that you load: every of those x lives in a context that is in $ContextPath, so they'd shadow each other. When you did put it after the EndPackage[] you would create it in the "calling" context, that would usually be Global` but could be different depending on how the package loaded. That wouldn't usually give you shadow messages but of course is bad style, since you do create global symbols. If you put it before the End[] it should not create shadow messages since the x will then be created in the private context and it will also not "leak" from the package. hth, albert