MathGroup Archive 2009

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

Search the Archive

Re: Mathematica scoping / contexts / variable localization

  • To: mathgroup at smc.vnet.net
  • Subject: [mg105061] Re: [mg105023] Mathematica scoping / contexts / variable localization
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Thu, 19 Nov 2009 05:25:13 -0500 (EST)
  • References: <200911181158.GAA04246@smc.vnet.net>

Hi Leo,

I certainly don't qualify as a guru (and don't want to), but here are my two
cents.

1. I usually think of a notebook as an interactive development and
visualization tool (apart from it being an interactive document, the aspect
which is more relevant for presentation rather than for development),
something like a top-level in some other functional languages. Modules and
other smaller-scale localization tools should be sufficient within a
notebook, and if they are not, it's a clear sign to me that the (part of
the) functionality must be moved into a properly formed package (at least
using BeginPackage - EndPackage etc).

2. Nobody forbids you to use BeginPackage - EndPackage with inner Begin-End
and private sub-contexts within a notebook, with the same benefits as they
give you in stand-alone package files.

3. A somewhat related question about automation of the steps needed to
localize some code in a certain context was recently asked on the group. I
posted some possible solution in this thread:

http://forums.wolfram.com/mathgroup/archive/2009/Aug/msg00701.html

It is not without bugs though - I mentioned some of the limitations (all of
which can be fixed) in my post. Eventually I will fix them and have a more
complete solution available. See if the solution from my post can serve your
needs.

Regards,
Leonid








On Wed, Nov 18, 2009 at 2:58 PM, Leo Alekseyev <dnquark at gmail.com> wrote:

> Dear Mathematica gurus,
>
> One of the things that initially made Mathematica difficult for me to
> use was scoping -- in particular, the fact that all symbols by default
> appear in the global namespace.  Even though this is a default
> behavior for interactive evaluation in many packages, e.g.
> R, in Mathematica, it leads to a greater potential for errors because
> unlike those languages, in Mathematica
> (1). a symbol can have multiple DownValues, and
> (2). if one forgets to explicitly localize a symbol inside a scoping
> construct, it may silently be taken from the global namespace.
>
> After many years I finally figured out a (more or less) clean way to
> structure my code and workflow, through a combination of defining
> modules, contexts, packages, and careful use of Clear and Remove.
>
> I still wonder, however, why there isn't a construct similar to Module
> that would define a unique private context for _all_ symbols within
> the construct (i.e. without having to declare them in a list).  You
> can kind of simulate this behavior by using BeginContext["MyCont`"]
> together with redefining $ContextPath temporarily to only have
> "MyCont`" and "System`".  This is obviously too verbose to be of
> practical use, but I do wonder why there isn't a built-in construct.
>
> I suppose my question is -- is there a deep wisdom behind its absence,
> or perhaps I am an anomaly in thinking that such behavior (automatic
> lexical scoping for symbols in subroutines, present in R and
> many others) would be incredibly handy?..
>
> Thanks,
> --Leo
>
>



  • Prev by Date: Re: Re: shadow warning with function parameters and local
  • Next by Date: Re: Permanent Computation Efficiency
  • Previous by thread: Mathematica scoping / contexts / variable localization
  • Next by thread: Re: Mathematica scoping / contexts / variable localization