Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: What is the point of having Initializations in DynamicModule and Manipulate?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg123057] Re: What is the point of having Initializations in DynamicModule and Manipulate?
  • From: Mike H <mike.honeychurch at gmail.com>
  • Date: Tue, 22 Nov 2011 05:33:54 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

On Tue, Nov 22, 2011 at 8:53 AM, John Fultz <jfultz at wolfram.com> wrote:

> Let's take the simpler case (which is no different in any fundamental
> way...it's
> just easier to step through in a hyper-detailed way).  Beginning with an
> evaluation in a fresh kernel of the following:
>
> DynamicModule[{c}, {plot},
> Initialization :> (g[x_] := Cos[x]; plot = Plot[g[x], {x, 1, 10}]),
> UntrackedVariables -> {g}]
>
> * Shift+Enter evaluation occurs.  It evaluates the above expression to
> itself
> (well, except an extra option added to DynamicModule, but that's not
> relevant to
> this discussion).  You can confirm this, incidentally, by wrapping
> InputForm
> around the expression and evaluating it in a fresh kernel.
>
> * The kernel calls MakeBoxes on the evaluated result.  The MakeBoxes
> result is
> pretty simple for this case, so I'll print it out here so you can see it.
>  Note
> the naked "plot".  This will never change from here on out, since it's not
> Dynamic.
>
> DynamicModuleBox[{c$$}, RowBox[{"{", "plot", "}"}],
>  Initialization :> (plot = Plot[g[x], {x, 1, 10}]),
>  DynamicModuleValues :> {}, UntrackedVariables -> {g}]
>
> * The kernel sends the boxes over MathLink to the FE.  As the FE reads the
> boxes, it decorates the variables with $CellContext` (which you see when
> toggling the cell expression).  At this point, the Shift+Enter evaluation
> has
> been fully completed as far as the kernel is concerned, and the FE has the
> entire result of the Shift+Enter evaluation in memory, but has yet to
> display
> it.
>
> * The FE tries to display the DynamicModuleBox, but stops when
> encountering the
> Initialization option.  It now sends the body of the Initialization option
> to
> the kernel and waits for the evaluation to finish.
>
> * Now that the evaluation is finished, the FE now draws the
> DynamicModuleBox's
> contents.  Of course, the contents are simply RowBox[{"{", "plot", "}"}],
> so the
> display of this static result didn't actually depend upon the code in
> Initialization.  Nonetheless, the FE was contractually bound to evaluate
> the
> Initialization code before displaying *anything* inside of DynamicModule.
>
> At this point, you'll see
>
> {plot}
>
> on the screen, and the symbols 'g' and 'plot' are now assigned in the
> kernel.
>
> Sincerely,
>
> John Fultz
> jfultz at wolfram.com
> User Interface Group
> Wolfram Research, Inc.
>
>
>
>

Thank you for explaining this evaluation sequence. I *does* explain to me
why I see what I see. Nevertheless would you concede that documentation
that says "When DynamicModule is first evaluated, initial assignments for
local variables are made first, and then any setting for the Initialization
option is
evaluated." would lead a user to expect something quite different to what
you have outlined? What you have described doesn't seem to be intuitive
given the documentation.

You indicated you were going to alter the documentation, so could I suggest
that something along the lines to what you have just posted would be
appropriate.

Finally can you please provide a description of the
option UntrackedVariables. I'd like to say that what it will do is
intuitive but given this thread I dare not.

thanks

Mike


  • Prev by Date: Re: special character size
  • Next by Date: Re: How to do quickest
  • Previous by thread: Re: What is the point of having Initializations in DynamicModule and Manipulate?
  • Next by thread: Re: What is the point of having Initializations in DynamicModule and Manipulate?