MathGroup Archive 1999

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

Search the Archive

another set of global variables

  • To: mathgroup at smc.vnet.net
  • Subject: [mg16740] another set of global variables
  • From: hanssen at zeiss.de
  • Date: Wed, 24 Mar 1999 02:23:52 -0500
  • Sender: owner-wri-mathgroup at wolfram.com

Hi, MathGroup,

I have written an algorithm (let's call it A), which is supplied with parameters. Some of them
are functions, some others are data (lengthy Lists of numbers). This algorithm calls the functions 
passed as parameters very often, with a lot of data being always the same (for one invocation of A). 
Therefore, I put those data into global Variables. The functions passed to A are aware of the names 
of those global variables and access them freely.

There is a golden programming rule is to avoid global variables, where ever possible. This rule
is trespassed here severely, but it was necessary because otherwise the data in those global variables 
- quite a lot of stuff - would have to be passed through parameters over and over again. Also A has 
some own global variables, which I like to inspect, if the algorithm A fails.

Now I am reaching the point, where I want to use A (with other functions as parameters and other
derived data) within A like this:

g[x_]:=....;

f[x_]:=(globalsOfG=...;(* think of this being many global variables, for brevity only one named here *)
           A[g[x],...]
          );

A[f[x],...];

Now, I am cought by my "bad programming habits" using global variables. Therefore my question:

Is it possible, to somehow influence the default "context", where global variables "live". Let's think
of a pair of functions PushGlobal and PopGlobal. Between PushGlobal and PopGlobal, all
global names previously accessible become inaccessible, except those, which are passed as parameters
to PushGlobal (as many, as one likes to pass to it). Global names created between PushGlobal and 
PopGlobal would be new, without possible interference to things with the same name existing 
outside of this range.

If something like this existed, I would rewrite the example above to

g[x_]:=....;

f[x_]:=
Module[{resultOfA}
      ,PushGlobal[g];	(* perhaps, x would also have to be pushed with g *)
       globalsOfG=...;	(* think of this being many global variables, as above *)
       resultOfA=A[g[x],...];
       PopGlobal[];
       resultOfA
               ];

A[f[x],...];


Thus, within f, all global names except g (being parameter of PushGobal), become inaccessible.
(Possibly, one would also have to include x, the parameter of f, depending, if a parameter
is considered a global entity or not).

Even if one of the (possibly many) names represented by "globalsOfG" existed in Global` before, a new 
instance is created for it, g accesses thes new instance. Also, any global names accessed in A
at run time would be considered new and they would not interfere with identical names
existing before PushGlobals. After PopGlobals, all names refer to the same entity, they 
referred to, before PushGlobals was called.

Any idea to this challenge? - Is this a "wheel", which needs not to be reinvented?

Kind regards

Dipl.-Math. Adelbert Hanszen



  • Prev by Date: Graphics Printing
  • Next by Date: Combining Lists
  • Previous by thread: Graphics Printing
  • Next by thread: Re: another set of global variables