Re: Shadow problem of a variable defined in a function context inside different packages
- To: mathgroup at smc.vnet.net
- Subject: [mg117976] Re: Shadow problem of a variable defined in a function context inside different packages
- From: Alexey <lehin.p at gmail.com>
- Date: Thu, 7 Apr 2011 08:04:03 -0400 (EDT)
Fonseca, The reason is that all symbols used in the input expression are created in the current $Context before evaluation of the input expression (unless the symbol is not defined in one of the contexts of $ContextPath). As I understand the value of In[$Line] is also assigned before evaluation of In[$Line]. So every of the symbols used in the input expression must be inevitably associated with some Context. If the symbol does not exist in the $ContextPath it is created in the current $Context (by default it is Global`). I hope someone in the newsgroup will correct me if I misunderstand something. Alexey P.S. I think these threads on Stack Overflow can be useful for you: http://stackoverflow.com/questions/4988427/accidental-shadowing-and-removedsymbol/4991503#4991503 http://stackoverflow.com/questions/4664091/package-import-problem-in-mathematica/4664741#4664741 Fonseca <public at fonseca.info> wrote: > 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? > > Thank you, > P. Fonseca