Re: Re: shadow warning with function parameters and local
- To: mathgroup at smc.vnet.net
- Subject: [mg105064] Re: [mg105029] Re: shadow warning with function parameters and local
- From: brien colwell <xcolwell at gmail.com>
- Date: Thu, 19 Nov 2009 05:25:49 -0500 (EST)
- References: <hdttip$hal$1@smc.vnet.net> <200911181159.GAA04325@smc.vnet.net>
Thanks Albert and Bobby, the context behavior is clearer to me now. But why does M have Module and Block? When is Module favorable? My current understanding is that both set up local contexts; however, unlike Block, Module touches or exports a symbol into the containing context. Albert Retey wrote: > 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 > >
- References:
- Re: shadow warning with function parameters and local module variables
- From: Albert Retey <awnl@gmx-topmail.de>
- Re: shadow warning with function parameters and local module variables