Re: Delay in $Context changes taking effect?
- To: mathgroup at smc.vnet.net
- Subject: [mg30068] Re: Delay in $Context changes taking effect?
- From: "Orestis Vantzos" <atelesforos at hotmail.com>
- Date: Thu, 26 Jul 2001 01:20:04 -0400 (EDT)
- Organization: National Technical University of Athens, Greece
- References: <9jb2e8$88k$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
A more "advanced", though not entirely satisfying, solution is the following Step 1) Load Utlities`Notation` and define the following notation: Notation[BoxHold[expr_] ==> BoxHeld[<blue>ToBoxes[expr_]<blue>]] the part between the <blue> tags must be wrapped in a MadeBoxesWrapper available through the Full Notation Palette, under Wrapper Boxes. Step 2) Define the function ReleaseBoxHeld: ReleaseBoxHeld[{context_,contextPath_},expr_BoxHeld]:= Block[{$Context=context,$ContextPath=contextPath}, ToExpression@expr] Step 3) Use it like this: ReleaseBoxHeld[{"myContext`",{"System`"}},BoxHold[mySymbol]] This creates (and returns) the symbol myContext`mySymbol This implementation avoids the phenomenon of "context polution", which is the creation of a "shadow" of a symbol (usually in the Global context) which is meant to be created in a different context. It does so by intervening at the parser level, hence the use of Notation, before any symbols are actually created. The problem is that the BoxHeld expression is invisible to all scoping constructs(Module,With,Block)! For instance With[{yo=hi}, BoxHold[yo]] will return BoxHeld["yo"] instead of BoxHeld["hi"]. I am working on several solutions right now, but nothing seams really satisfying.. Orestis "Michael" <michael at science.edu> wrote in message news:9jb2e8$88k$1 at smc.vnet.net... > I am trying to use the $Pre and $Post functions to alter the context for > variables, so that each notebook has its own set of local variables. > However my attempts have not succeeded. I think that $Context is supposed > to be the default context new variables are created in, and $ContextPath the > paths to search for existing variables in. After a lot of research, it > appears that the problem is that changes to $Context and $ContextPath do not > take effect until after the current line of input in a notebook is > processed. > > To illustrate, if I put a BeginPackage on a separate line from the > assignment, the newly created variable is created in the expected context. > > In[1]:= Remove[f, Test`f] > > In[2]:= > BeginPackage["Test`"]; > f = 1; > EndPackage[]; > > In[3]:= Context[f] > Out[44]= "Test`" > > However, if the BeginPackage is on the same line, or in a > CompoundExpression,Block,Module,If, etc. then the newly created variable is > created in the wrong context: > > In[46]:= Remove[f, Test`f] > > In[47]:= BeginPackage["Test`"]; f = 1; EndPackage[]; > > In[48]:= Context[f] > Out[48]= "Global`" > > I say the "wrong" context because there is nothing documented (that I can > find) which suggests that changing $Context or $ContextPath will result in > any delay until the change takes effect. For what I am doing, it is > essential that the context change immediately, otherwise all my effort is > for nothing. > > Can anybody tell me what I might be doing wrong, or perhaps an alternate > method to accomplish my goal? > > Thanks, > > Michael > > >