MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: shadow warning with function parameters and local module variables

  • To: mathgroup at smc.vnet.net
  • Subject: [mg105170] Re: shadow warning with function parameters and local module variables
  • From: dh <dh at metrohm.com>
  • Date: Mon, 23 Nov 2009 06:51:59 -0500 (EST)
  • References: <hdttip$hal$1@smc.vnet.net>


brien colwell wrote:

> hi all,

> 

> 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?

> 

> thanks,

> Brien

> 

> 

> In[1]:=

> BeginPackage["a`"];

> Test1[t_] := Module[{v = t + 1}, t + v];

> EndPackage[];

> BeginPackage["b`"];

> Test2[t_] := Module[{v = t + 2}, t + v];

> EndPackage[];

> 

> Out[1]:=

> v$::shdw: Symbol v$ appears in multiple contexts {b`,a`}; definitions in 

> context b` may shadow or be shadowed by other definitions. >>

> 

> 

> 

> 

Hi Brien,

I am no expert in this but my understanding my help you anyway.

Consider:

1)

Module[{t1},

  Print[t1];

  ]

2)

Block[{t2},

  Print[t2];

  ]

Neither 1 nor 2 has a context on their own. Both create a symbol in the 

active context (e.g. Golbal`).

1) creates a new local, temporary variable by adding  $nnn, where nnn is 

a number that is incremented each time the module is executed, to the 

(Global`) variable t1.

2) does not create a new variable, but gives the (Global`) variable t2 a 

temporary new value (empty in our case where we did not give an initial 

value). The new value is also valid in any function that is called from 

inside Block.



Daniel






  • Prev by Date: Re: Labelling a plot with maximum
  • Next by Date: Re: compositions with the restricted set of integers
  • Previous by thread: Re: Re: shadow warning with function parameters and local
  • Next by thread: Re: Preserve notebook's $ContextPath between sessions /