The cause is the variable name confusion. It is true that each DynamicModule
creates different symbol names. However, a new symbol name is generated again in Dynamic
. For example, try running this code and slide the slider:
DynamicModule[{x = 0, f}, f[] := Print[SymbolName@f];
f[]; Slider[Dynamic[x, f[] &]]]
(* f$400707 *)
(* Slider *)
(* f$$3152 *)
Or, usingf$$3152
is printed when you move the slider.
Using OP's code construction (added a slider in outer
for clarity)+ some modifications:
ClearAll[inner, outer];
inner[Dynamic[x_, args___]] := DynamicModule[{update},
update[] := (Print@update; Print@args);
update[];
Slider[Dynamic[x, update[] &]]];
outer[Dynamic[x_, args___]] := DynamicModule[{update},
update[] := (Print@update);
update[];
{Slider[Dynamic[x, update[] &]],
inner[Dynamic[x, update[] &]]}];
x = 1;
outer[Dynamic@x]
(* update$405810 *)
(* update$405811 *)
(* update$405810[] & *)
(* 2 Sliders *)
(* FE`update$$3176 *)
(* FE`update$$3177 *)
(* FE`update$$3177[] & *)
The 3176
is printed when you move the first slider (outer
). The two 3177
s are printed when you move the second slider (inner
). The first three lines of output is the expected behavior (args
refers to update
in outer (405810)
not inner (405811)
), which is correct, but the last three lines are the actual behavior (args
refers to update
in inner (3177)
not outer (3176)
).
This creates name confusion as I stated in the first comment, forcingcausing Mathematica to use the definition of update
defined by inner
for all instances of update
.