This discussion is completely in the context of DynamicModule
, which appears to be irrelevant when we consider the following examples.
x := Print["hello"];
ToString @@ Unevaluated /@ Dynamic[x]
and
y[] := Print["hello"];
ToBoxes[ToString @@ Unevaluated /@ Dynamic[y[]]]
Where in the first example x
is evaluated, but in the second example y[]
is not. So while Michael is probably right that the behavior occurs because we want more speedy evaluation, it does not have to do with DynamicModule
.
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.