Skip to main content

Dynamic Symbol tracking from a list of Symbols


A minimal example for the Bonus in Can a Dynamic be attached to the single elements of a list?


DynamicModule[{foo = Hold[$1, $2], $1 = 0, $2 = 0},
{
Dynamic[Print["redrawing first"]; foo[[1]]],
Dynamic[Print["redrawing second"]; foo[[2]]],
Button["first", ++$1],
Button["second", ++$2]

}
]

Observe the Messages window as these buttons are pressed. The redraw messages are printed independently after the initial evaluation, i.e. you can get a message list like:



redrawing first


redrawing second


redrawing first


redrawing first


redrawing first



redrawing first


redrawing second



(corresponding to 4, 1, in the expression)


By what mechanism does Mathematica track the value of $1 or $2 changing and update each Dynamic individually despite neither Symbol appearing explicitly in the bodies?



Answer



From:


tutorial / AdvancedDynamicFunctionality / Automatic Updates of Dynamic Objects



[...] If a variable value, or some other state of the system, changes, the dynamic output should be updated immediately. [...] It is critical that dependencies be tracked so that dynamic outputs are evaluated only when necessary.




And the most important part:



[...] the system keeps track of which variables or other trackable entities are actually encountered during the process of evaluating a given expression. Data is then associated with those variable(s) identifying which dynamic expressions need to be notified if the given variable receives a new value. [...]



And how this association/building dependency tree is performed can be adjusted by TrackedSymbols option. By default it is All so it will be scanning and gathering symbols needed when foo[[1]] is evaluated.


For TrackedSymbols -> Full it will stop on a visible level, so only changes to foo itself will trigger changes.


Comments