Re: shadow warning with function parameters and local module variables
- To: mathgroup at smc.vnet.net
- Subject: [mg105029] Re: shadow warning with function parameters and local module variables
- From: Albert Retey <awnl at gmx-topmail.de>
- Date: Wed, 18 Nov 2009 06:59:45 -0500 (EST)
- References: <hdttip$hal$1@smc.vnet.net>
Hi, > I have a set of fairly complex packages. Frequently I use the same > variable names in function definitions and local modules across many > packages. Sometimes I see this type of warning: > > <varname>::shdw : Symbol <varname> appears in multiple contexts {*}; > definitions in context * may shadow or be shadowed ... > > This does not happen with symbols in the package context (already > fixed); it happens only with function parameters and variables in > local modules. I can recreate the warning with the following code > snippet. It seems to me that the variable 'v' should be safely local > to each module. Is there really a symbol conflict to worry about > here? no, the variables v and t are safely localized by SetDelayed and Module, so everything would work as expected, on the other hand... > In[1]:= BeginPackage["a`"]; > Test1[t_] := Module[{v = t + 1}, t + v]; > EndPackage[]; > BeginPackage["b`"]; > Test2[t_] := Module[{v = t + 2}, t + v]; > EndPackage[]; ...the correct way to make the variable v and t private to the packages would be as follows: BeginPackage["a`"]; Test1::usage="Test1[t] does something."; Begin["`Private`"] Test1[t_] := Module[{v = t + 1}, t + v]; End[]; EndPackage[]; that is you would mention all symbols to be exported by your package between BeginPackage and Begin, then put the function definitions between Begin and End. Usually it is recommended to define the usage messages between BeginPackage and Begin, but actually it is good enough to just create the symbols there, so they are in the correct package... If you are interested, you might want to learn about how Mathematica handles name spaces (see Context, BeginPackage, Begin etc.) and localization via Module, Block and With (and of course some other functions). hth, albert
- Follow-Ups:
- Re: Re: shadow warning with function parameters and local
- From: brien colwell <xcolwell@gmail.com>
- Re: Re: shadow warning with function parameters and local