Re: Scoping constructs Block, Module, ModuleBlock violate
- To: mathgroup at smc.vnet.net
- Subject: [mg111156] Re: Scoping constructs Block, Module, ModuleBlock violate
- From: John Fultz <jfultz at wolfram.com>
- Date: Thu, 22 Jul 2010 05:42:29 -0400 (EDT)
On Wed, 21 Jul 2010 07:11:41 -0400 (EDT), Michael wrote: > On 7/20/2010 2:40 AM, Patrick Scheibe wrote: >> Hi, >> >> even *in* a DynamicModule you have to use Dynamic to *mark* the that >> should be >> updated dynamically >> > On 7/20/2010 4:55 AM, Istv==E1n Zachar wrote: >> I think the answer for your DynamicModule-related question is: >> DynamicModule *does not* wrap its local variables in Dynamic. It is >> never stated in the Documentation. >> > Ah of course... silly mistake. > > I originally was trying to use the Block[] approach, because I don't > really want the full functionality of DynamicModule[] - for instance the > persistance between sessions, as this code is eventually going in a > pop-up window, and the inability to easily share the variable outside > the scope of the dynamic module. But I guess I can make it work this > way... :) > > I suppose the surprise of Block variables disappearing inside Dynamic > really threw me for a curve, and I am still puzzled as to what is going > on. In my opinion if these two don't work together it really should be > documented under both Dynamic and Block under the "Possible Issues" > section. > > > Thank you both for your help. > > Michael DynamicModule has an option called UnsavedVariables which you can use for what you want. I didn't look at your original code, but I think I probably know why your use of Block and Dynamic together failed. The principal point is that Block introduces an environment where variables can be temporarily redefined, but that environment evaporates the moment Block is finished evaluating. The contents of a Dynamic never even begin evaluating until well after the Shift-Enter evaluation is over. So, if your code were like this: Block[{x == 5}, Dynamic[x]] then it's kind of like expecting this to return 5: Block[{x == 5}, 0]; x because the evaluation of the contents of Dynamic is so out-of-band, it might as well be like a completely different evaluation. And, in this case, the syntax coloring is a huge indicator that something probably unwanted is going to happen. Sincerely, John Fultz jfultz at wolfram.com User Interface Group Wolfram Research, Inc.