Re: Module inside Module. Conflict between inner Module local variable with outer module parameter
- To: mathgroup at smc.vnet.net
- Subject: [mg79583] Re: Module inside Module. Conflict between inner Module local variable with outer module parameter
- From: Christian & Nicky Chong-White <christian_chongwhite at hotmail.com>
- Date: Mon, 30 Jul 2007 06:42:37 -0400 (EDT)
- References: <f8h4k8$6s5$1@smc.vnet.net>
Hi Nasser Sorry I can't provide a solution but I support your question as I have found this issue too. One develops an understanding of scope with programming languages and this difference in Mathematica is a source of frustration. A somewhat source of relief or understanding is that the new context colouring in V6 correctly indicates the scope Mathematica applies. BTW I came across this issue when working with Dynamic interactivity with the related construct, DynamicModule. See my question to this group titled "DynamicModule and InheritScope." http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread/thread/53d30e951048a00e?hl=en I hope someone helps you out! :-) Cheers Christian Nasser Abbasi wrote: > This Mathematica 6.0.1 > > I just found out that I can't declare a local variable inside a Module > to be the same name as an input parameter for the outer module. > > foo[i_] := Module[{}, > > boo[] := Module[{i = 1}, > Print[i]; > ]; > > boo[]; > ] > > It seems an inner Module is being treated on the same level as the > outer module as far are variable scope is concerned. When I call > foo[5] for example, the local variable 'i' for boo[] was set to 5, > when it was supposed to be LOCAL to boo[] ! It looks like local > variables to inner modules are treated just like they are local > variables to the outer module. > > I understand that one can't make a local variable with the same name > as the argument, but boo[] above is supposed to be a separate module > (even though it is an inner module) except its scope is limited to > inside foo[]. > > So I do not see why the above would not be allowed. This restriction > does not seem to make too much sense to me. > > Is there a trick to make the above legal without moving boo[] outside > of foo[] ? > > thanks, > Nasser