Google Groups

Re: Manipulate inside module

John Fultz Sep 12, 2011 1:23 AM
Posted in group: comp.soft-sys.math.mathematica
On Sat, 10 Sep 2011 07:30:40 -0400 (EDT), Charles Wells wrote:
> I expected the following expression to show the square of the current
> value of the slider, but it shows only a^2.  There is clearly something I
> don't understand about Module.  Any insights?
> Module[{b = a}, (Print[b]; Manipulate[b^2, {{a, 1}, 0, 5}])]

I could explain in some considerable detail exactly what's going on here (and it
would be considerable), but it might be more useful and straightforward to give
you a very simple rule...

Module variables should *never* appear inside Dynamics or Manipulates internal
to that Module.

To be clear with some examples (all using Dynamic, but they could equally well
use Manipulate, which is implemented using Dynamic)...

(* OK *) Dynamic[Module[{a}, a]]
(* OK *) Module[{a},
                (* expression involving a*);
                Dynamic[(* expression *not* involving a *)]
(* BAD *) Module[{a}, Dynamic[a]]

In fact, this rule is so straightforward and universal, there's no reason that
we shouldn't have some advisory syntax highlighting which points out the
badness...I intend to do this for a future release.


John Fultz
User Interface Group
Wolfram Research, Inc.