I'm putting my new copy of Mathematica 10 through its paces and I noticed a weird change in the colouring scheme that I find very annoying, and I'd like to get some insight into why it's happening.
The change happens, as far as I can tell, for variables that have been set in some scoping construct which are then used inside of a Manipulate command. Thus, for example, the code
Module[
{A = 1},
Manipulate[
Plot[
A Sin[k x]
, {x, 0, 20}
]
, {k, 1, 10}
]
]
renders differently in the Mathematica 9 and 10 frontends:
Playing around in Edit > Preferences > Appearance > Syntax Coloring > Errors and Warnings, the error class that this is attributed to is
Variables that will go out of scope before being used
though what that means exactly isn't very clear to me. Therefore, I have some questions:
Is this in fact an incorrect usage? If so, why, and what alternative construct should I use? (I notice, in particular, that in version 10 the red colour is displayed for Block and Module but not for With.)
Why did this only start happening in version 10? Which version is right?
If this sort of code is OK, can I safely disable this sort of error marking? What risks does that entail?
Module
withinManipulate
(and I think nesting it the other way around is what caused the syntax warning because the Manipulate did not play nice with the Module outside of it). Or as you said you can useWith
, and in this case it's acceptable to do so asA
is basically just a constant that does not change its value when the code is run. $\endgroup$Module
inside theManipulate
) is preferable? $\endgroup$Module
withinManipulate
, yourModule
variables can depend on the manipulate parameter, whereas you cannot achieve this the other way around (as the manipulate parameter disappears outside of the scope of the Manipulate block). You can try this to see what I'm talking about: 'Manipulate[Module[{A = k}, Plot[A Sin[3 k x], {x, 0, 20}]], {k, 1, 10}]. Of course in your case that's not going to be important since your Module variable does not change, hence the use of
With` will be just fine. $\endgroup$Module[{A}, Manipulate[A = k; Plot[A Sin[k x], {x, 0, 20}], {k, 1, 10}]]
works fine in both v9 and v10, but only v10 colours it as incorrect syntax. $\endgroup$DynamicModule
I think I now figured out what the problem with Module+Manipulate probably is: Dynamic content (likeManipulate
) is stored across sessions. However a normalModule
variable is not stored that way. I guess that would create problems as soon as you save the notebook and later open it in a new instance of Mathematica. $\endgroup$