# TrackedSymbols affected by CurrentValue. Another problem with SetDelayed(:=)+OwnVales in Initialization

It is MWE, only a Disk[] that should print "Click" when clicked.

Also "date... updating" should be printed on the first evaluation and on each update of variable enabled. (which is constant in this MWE)

In the Column there is another Dynamic object which shows current MousePosition. And this element is responsible for printing (flooding) "date... updating" even though enabled is not changing.

DynamicModule[{enabled = True, pos},
Column[{
Dynamic[Print[DateString[] <> " updating"];
EventHandler[Graphics@Disk[], {"MouseClicked" :> Print["Click"]}],
TrackedSymbols :> {enabled}]
,
Dynamic[pos]
}]
, Initialization :> (pos := MousePosition["Graphics"])]


I think I don't understand this and IMO it is not expected or desired behaviour.

When one put another Print next to Graphics you will see that it is in face whole Graphics updating.

So the question is:

If it's not a bug (I missed something) then how to recreate this MWE so it behaves correctly.

I think it may be (but not necessarily) related to Why does a heavy background slow LocatorPane from updating, even if only the locators are Dynamic?

p.s. Dynamic[Refresh[..., TrackedSymbols:>{enabled}]] behaves the same way.

Edit so at the end it seems to be more related to Button evaluation inside DynamicModule

-
Ah I finally understand, somehow I read constant as constantly, which is quite different :P. – Jacob Akkerboom Mar 5 '14 at 14:50
Hm see update, maybe it has something to do with DynamicModuleValues. – Jacob Akkerboom Mar 5 '14 at 15:18

(This is more of an extended comment until a better understanding evolves.)

Finally I've found the relevant MathGroup thread from old - apparently the behaviour is as old as the dynamic interactivity in Mathematica. At that time, Norbert Pozar introduced a really simple example to demonstrate the strange internals of DynamicModule when a symbol in an Initialization assignment sets OwnValues instead of DownValues with SetDelayed (Set is fine). Be aware, this will probably crash your kernel!

DynamicModule[{x}, 1, Initialization :> (x := Print["Should never see this."])]


However, changing the assignment to something[]:=... works as expected. Clearly, DynamicModule evaluates the symbol x internally, even if it is never used; and if the symbol has side effects, that can be catastrophic.

Bottom line: always use DownValues instead of Onwvalues when doing assignments inside Initialization code in DynamicModule. Like that:

DynamicModule[{x}, 1, Initialization :> (x[]:= Print["Should never see this."])]


Spelunking in the definition of DynamicModule, I've found that the problem is even more deeply embedded: it must be in DynamicModuleBox. The following example still crashes a fresh kernel:

CellPrint@Cell@BoxData@DynamicModuleBox[{x}, 1,
Initialization :> (x := Print["Should never see this!"])]

-
Great thread! Thanks. – Kuba Mar 6 '14 at 22:51
@Kuba Unfortunately, no one from WRI replied to that MG thread so the only option we have is to explore the definition of DynamicModule. – István Zachar Mar 6 '14 at 22:55
I've pined JohnFultz in chat yesterday. I would love to see an explanation. p.s. added example for clarity, hope you don't mind. – Kuba Mar 6 '14 at 22:57
p.s. OwnValues are ok with Set, worth adding in bottom line that it is in context of SetDelayed. – Kuba Mar 6 '14 at 23:03

Maybe this does what you want?

DynamicModule[
{enabled = True, pos},
Column[
{
Dynamic[
Print[DateString[] <> " updating"];
EventHandler[
Graphics@Disk[], {"MouseClicked" :> Print["Click"]}
]
]
,
Dynamic[pos[]]
}
]
,
Initialization :> (pos[] := MousePosition["Graphics"])
]


Really... trial and error.

-
That seems hopeful, doesn't it? I'm not exactly sure what you mean. – Jacob Akkerboom Mar 5 '14 at 15:09
Just talking :P it's something but it will not help me, the main issue is still there. Now testking your answer... – Kuba Mar 5 '14 at 16:16
So the answer is: never ever use SetDelayed for OwnValues purposes of DynamicModule variables. Why? Everything is crazy then. – Kuba Mar 5 '14 at 16:21