Is the following behaviour a bug? It's the same in all versions between 9.0-10.2.0.
Let's try this simple Manipulate
with the initial value of a
set to 1
:
Manipulate[Style[a, Large], {{a, 1}, 0, 2}, SaveDefinitions -> True]
Now let us do the same, but set a value to the global a
first:
a = 0.5;
Manipulate[Style[a, Large], {{a, 1}, 0, 2}, SaveDefinitions -> True]
The initial value of the local a
(set to 1
in the Manipulate
) is hijacked by the global value of a
. It now shows as 0.5
instead of 1
.
This only happens with SaveDefinitions -> True
.
Is this a bug or is it expected behaviour?
Analysis: As Michael points out in the comments, the SaveDefintions
option causes a=0.5
to be recorded in the Initialization
option. The Manipulate
evaluates to this InputForm:
Manipulate[Style[a, Large], {{a, 0.5}, 0, 2}, Initialization :> {a = 0.5}]
Normally SaveDefinitions -> True
is supposed to record every symbol that's used in the Manipulate
, but apparently it neglects to filter out those that are used as parameters.
So is it reasonable to record the global value of a
? In some cases it might be. Consider
a = 1;
f[] := a
Manipulate[f[] + a, {a, 0, 1}]
Is this Manipulate
going to show a + 1
as a
goes from 0..1, or is it going to show a + a
?
It shows a + 1
, as one would expect based on the fact that the parameter is local to the Manipulate
and shouldn't conflict with global variables. But the function f
depends on the global a
, so if we want to preserve f
, we also need to preserve the global a
. Thus on first sight it seems reasonable that SaveDefinitions
should save the global a
(but not that that should influence the local value).
Now let us see what actually happens if we try it:
Manipulate[f[] + a, {a, 0, 1}, SaveDefinitions -> True]
Well, it seems SaveDefinitions
changed the behaviour completely. Now it shows a+a
instead of a+1
... I would definitely call this a bug!
a
in the code triggers a search for a symbolGlobal`a
with a value (perhaps) that is packaged into theInitialization
option. Absolutely no reason I see that such behavior is appropriate here. $\endgroup$a=1; f[] := a; Manipulate[f[] + a, {a,0,1}]
show the value ofa + a
or1 + a
? It depends on whether we useSaveDefinitions -> True
! $\endgroup$f
(withSaveDefinitions -> True
)! Try pasting several outputs into your notebook. The functionf
is redefined each time to represent the locala
in the last pasted Manipulate -- for all the other Manipulates! $\endgroup$