# Why won't this PrintTemporary[Dynamic[…]] expression work in a function?

I found myself reusing this code all the time to monitor variables during execution, so I'm trying to make it into a function:

pdr[expr_, symbols_List: {}, interval_: \[Infinity]] :=
PrintTemporary[
Dynamic[Refresh[expr, TrackedSymbols :> symbols,
UpdateInterval -> interval]]];


But then when I try to use it, as with something like

s={};
pdr[s,{s},1];

Do[
AppendTo[s,n];
Pause[1];
,{n,3}]


it doesn't seem to work. If I paste in the exact same PrintTemporary line that the function is calling in place of my pdr function, it works as expected. It seems like there's some quirk of Mathematica I'm overlooking here, so any help appreciated.

The argument {s} in pdr[s, {s}, 1] evaluates to {{}}. So the TrackedSymbols option of Refresh does not see a list of symbols.

One fix:

Clear[s];
pdr[s, {s}, 1];
s = {};

Do[AppendTo[s, n];
Pause[1];, {n, 3}]


Another:

ClearAll[pdr];
SetAttributes[pdr, HoldAll];
pdr[expr_, symbols_List: {}, interval_: \[Infinity]] :=
Block[symbols,
PrintTemporary[
Dynamic[Refresh[expr, TrackedSymbols :> symbols,
UpdateInterval -> interval]]]
];

• Ah, perfect. Thanks! – Trev Aug 18 at 17:09