Re: Manipulate inside module
- To: mathgroup at smc.vnet.net
- Subject: [mg121351] Re: Manipulate inside module
- From: John Fultz <jfultz at wolfram.com>
- Date: Mon, 12 Sep 2011 04:19:56 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Reply-to: jfultz at wolfram.com
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.
Sincerely,
John Fultz
jfultz at wolfram.com
User Interface Group
Wolfram Research, Inc.