The Advanced Dynamic Functionality tutorial informally describes a ticklish function as follows:
Every time you evaluate
RandomReal[]
, you get a different answer, and you might think thatDynamic[RandomReal[]]
should therefore constantly update itself as fast as possible. But this would normally not be useful, and would in fact have negative consequences for a number of algorithms that use randomness internally[...]For this reason,
RandomReal[]
is not "ticklish", in the sense that it does not trigger updates[...]The function
Clock
is intended specifically as a time‐based function that is ticklish.
How to tell if a function is ticklish? What makes it such?
Naturally, it's easy to see that Clock[]
is ticklish, as is another function that evaluates Clock[]
, even if it discards the intermediate result. Thus ticklishness in not a property of the function's symbol (this rules out an attribute, option, or a list of predefined ticklish function heads):
MyClock[]:=Clock[];
MyDateString[]:=(Clock[]; DateString[]);
then
Dynamic[Clock[]]
Dynamic[MyClock[]]
Dynamic[MyDateString[]]
Dynamic[DateString[]]
The first three output cells update dynamically, while the fourth does not. How do they know they need to update? At the first sight, there are differences in the box form of the last two output cells:
Cell[BoxData[
DynamicBox[ToBoxes[
$CellContext`MyDateString[], StandardForm],
ImageSizeCache->{141., {1., 10.}}]], "Output",
CellLabel->"Out[131]="]
Cell[BoxData[
DynamicBox[ToBoxes[
DateString[], StandardForm],
ImageSizeCache->{141., {1., 10.}}]], "Output",
CellLabel->"Out[132]="]
but that aha! was a total flop, as removing the $CellContext
from the first cell of the two did not make it non-ticklish, while adding it to the second simply broke its display red with a peculiar error message
DateString::shdw: Symbol DateString appears in multiple contexts {Notebook$$18$101847`,System`}; definitions in context Notebook$$18$101847` may shadow or be shadowed by other definitions.
- (caveat scrutor:
System`DateString
seemed to have disappeared after that, so I had to restart the kernel to proceed at this point, even though I Remove[]d the definition in theNotebook$$18$101847`
context; you may not want to repeat the experiment and simply take my word for it at this point.)
And in fact, simply writing the explicit box form into the notebook
NotebookWrite[EvaluationNotebook[],
Cell[BoxData[ToBoxes[Dynamic[MyDateString[]]]], "Output"]]
generates a self-updating cell.
So what does make a function ticklish?