I don't know if I like my interpretation of the examples below anymore. I will just leave them here though
Here is more crazyness. I think Michael explains quite well what happens. The only thing new this first section shows is that new symbols get created sometimes, which makes it seem functions point to the wrong thing, but that actually does not matter.
DynamicModule[{button, x = 0, ff, `},
ff := (++x);
var = Hold[x];
var2= z;
button = Button["setX", ff];
Dynamic[Column@{button, x, Hold[x], Hold[x], var, OwnValues[ff], z, var2}]
]
Hold[FE`x$$213]
Hold[FE`x$$213]
Hold[x$314]
{HoldPattern[FE`ff$$213] :> 1}
FE`z$$225
z$1236
Conclusions: It is possible for a variable to point to the wrong thing, like with var, or var2. ff does point to right thing, but it's code gets evaluated. Probably: The pointing to the wrong thing is not caused by the DynamicModule
changing it's x all the time. I suppose it really wants to evaluate the code attached to symbols and not have it refer to any of the used symbols.
DownValues
Also note
DynamicModule[{gg = 0, x},
OwnValues[gg] = {HoldPattern[gg] :> x};
Dynamic[{OwnValues[gg], Hold[x]}]
]
{{HoldPattern[FE`gg$$270]:>FE'x$$270},Hold[FE`x$$270]}
but
DynamicModule[{gg = 0, x = 0},
OwnValues[gg] = {HoldPattern[gg] :> x};
Dynamic[{OwnValues[gg], Hold[x]}]
]
{{HoldPattern[FE`gg$$271]:>0},Hold[FE`x$$271]}
It seems it does not matter if we set something using OwnValues
or in the regular way, using Set
or SetDelayed
. It seems that a function definition can even be changed afterwards, like in
DynamicModule[{gg = 0, x},
OwnValues[gg] = {HoldPattern[gg] :> x};
x = 0;
Dynamic[{OwnValues[gg], Hold[x]}]
]
{{HoldPattern[FE`gg$$312]:>0},Hold[FE`x$$312]}
Order of evaluation
The order of evaluation seems only to depend on the order in which they appear in the list of local symbols in the first argument of DynamicModule
.
DynamicModule[{gg, x},
x := (Print["x"]; {2});
gg := (Print["gg"]; First@HoldComplete[x]; Print["ggDone"]);
]
gg (*print*)
x (*print*)
ggDone (*print*)
x (*print*)
Null
and
DynamicModule[{gg, x},
x := (Print["x"]; {2});
gg := (Print["gg"]; First@HoldComplete[x]; Print["ggDone"]);
]
x (*print*)
gg (*print*)
x (*print*)
ggDone (*print*)
Null (*print*)
We also see that it uses the "old" definitions of symbols to generate the definitions of the new symbols. I suppose it never does use a new definition in the definition of another new symbol.
I am lost :). I don't think I like this much.
SetDelayed
doesn't work inside theDynamicModule
. $\endgroup$ff[]:=...
and thenff[]
instead offf
inside the button code, and that will work. This does not explain however whyOwnValues
don't work. $\endgroup$x
is assigned some random matrix, andf
is out of the game for theOwnValues
scenario. You can look at the generated box expression in the FE for your button, to see this. However, forDownValues
, while the assignment is again performed immediately, theDownValues
get stored byDynamicModule
with the help ofDynamicModuleValues
option. This keepsf[]
in the button code operational. $\endgroup$Dynamic
can be not quite intuitive. I usually find workarounds for what I need from it, but I can't say that I have any deep understanding of it. $\endgroup$