Skip to main content

Timeline for Scope within Context

Current License: CC BY-SA 3.0

22 events
when toggle format what by license comment
Sep 10, 2012 at 13:11 history edited jVincent CC BY-SA 3.0
Fixed a lot of ticks
Sep 10, 2012 at 12:14 vote accept Karsten W.
Sep 10, 2012 at 12:13 comment added Karsten W. Thanks, together with Block[{$Context="MyPackage'Private'"}, ToExpression[#][5]] & /@ tests found here I think I can build a setInContext and useInContext function. Thanks!
Sep 10, 2012 at 12:08 history edited jVincent CC BY-SA 3.0
Slight code cleanup
Sep 10, 2012 at 12:03 comment added jVincent @LeonidShifrin So while you could call it delayed parsing, it's actually a result of the way MakeExpreesion and MakeBoxes handle contexts. You could say that Evaluation of MakeExpression is parsing, but the parsing/evaluation distinction is fuzzy in that case.
Sep 10, 2012 at 12:02 comment added jVincent @LeonidShifrin Actually this is not the case. What You parse the expression during evaluation which is what puts the symbol into Global. Later when you use MakeExpression, it strips any contexts that appear in ContextPath, since they are redundent. But this can be abused by changing ContextPath, since MakeExpression will put any symbols not found in the contextpath into $Context.
Sep 10, 2012 at 12:00 comment added Leonid Shifrin I like your new answer better, although I would still phrase the explanation differently to emphasize the distinction between parsing stage and evaluation stage. As to your new ContextScope function, I use something similar but IMO more robust and based on BeginPackage - EndPackage, which is described in e.g. this answer - but I don't see any obvious issues with your implementation.
Sep 10, 2012 at 11:58 history edited jVincent CC BY-SA 3.0
added 1 characters in body
Sep 10, 2012 at 11:46 history edited jVincent CC BY-SA 3.0
added 39 characters in body
Sep 10, 2012 at 11:40 history edited jVincent CC BY-SA 3.0
added 287 characters in body
Sep 10, 2012 at 11:33 history edited jVincent CC BY-SA 3.0
added 287 characters in body
Sep 10, 2012 at 11:27 comment added Leonid Shifrin ... is what makes it non-trivial. Also, since people tend to read from top to bottom, I would remove the older statement on the impossibility of context changing from the answer altogether. Finally, converting to boxes still causes the initial parsing and creation of these symbols also in the Global` context - I actually mentioned this in the answer I linked to.
Sep 10, 2012 at 11:25 comment added Leonid Shifrin Well,this is more or less what I suggested here. I would not say "during it's evaluation all symbols will apear before any evaluation" - this is during parsing, not evaluation. It is important to maintain the distinction. Also, in your new suggestion (as well as mine I linked to), what really happens is that by converting to boxes you delay the parsing until later time when the context has been already changed, so you change the order of parsing and (part of) evaluation. This..
Sep 10, 2012 at 11:07 comment added jVincent @LeonidShifrin I have corrected my answer to reflect my newfound understanding of the problem thanks to your correction. Thank you. :)
Sep 10, 2012 at 11:06 history edited jVincent CC BY-SA 3.0
Corrected incorrect conclusion
Sep 10, 2012 at 11:02 comment added Leonid Shifrin I actually did not first notice your statement about the impossibility of dunamically changing the current context (for which the examples apparently serve as an illustration), and so initially viewed your two examples just as empirical observations of these different behaviors, thus my comment on entities multiplication :-).
Sep 10, 2012 at 10:58 comment added Leonid Shifrin Well, I just meant Occam's razor principle, in that it is always best to describe the principle from which say 10 different observations follow, than to only describe the observations alone, which would then look like magical spells.
Sep 10, 2012 at 10:55 comment added jVincent @LeonidShifrin I stand corrected. It is indeed because x is scoped upon its first appearance when the entire expression is first evaluated, which scopes it to global. Could you elaborate on "would not multiply entities" ?
Sep 10, 2012 at 10:52 comment added Leonid Shifrin "I don't think you can start and stop a context within other evaluations" - sorry, but this is simply not the case, see e.g. this answer. It is actually this feature of dynamic control over the current context and other things affecting parsing ($ContextPath) which make the encapsulation mechanism of Mathematica much more flexible and powerful.
Sep 10, 2012 at 10:48 comment added Leonid Shifrin Good observation, but I would not "multiply entities". The reason for the difference is in how expressions entered in cells in the FrontEnd are parsed. The parser makes an exception for the top-level CompoundExpression (second case), parsing line-by-line - thus this works. For the first case, it parses entire body of CompoundExpression before evaluating, and therefore the symbol x is still created in the Global` context. This has been explained also here.
Sep 10, 2012 at 10:46 history edited jVincent CC BY-SA 3.0
edited body
Sep 10, 2012 at 10:38 history answered jVincent CC BY-SA 3.0