[CMake] lexical scoping

Alan W. Irwin irwin at beluga.phys.uvic.ca
Sat Nov 3 15:16:09 EDT 2007


On 2007-11-03 12:27-0400 Bill Hoffman wrote:

> Declaring a variable as local is done in perl.  Which had the same sort of 
> problem.  By default all variables in perl are global.  You have to put a 
> "my" in front of the variable to specifically ask it to be a local variable.
>
> I think this will be used a lot with existing modules that set plenty of 
> global variables, and just adding a local_scope around some existing code 
> will break it if you make all set's automatically create a new local 
> variable.

First, let me say I think adding local scope is a great idea that will make
CMake a much more robust language. Also, it appears the debate is now
between Bill's idea of a command with a specific list of locally scoped
variables or the alternative of a command that locally scopes all variables
with some mechanism to specify the exceptions that have global scope.

Bill, I can see why you are worried about breaking existing modules where
you are not sure of what macro variable have been used by some users and
what has not, but in fact you cannot tell how CMake users have used macro
variables up to now.

Thus, the only completely safe thing to do for backwards compatibility is to
never use local scope of any kind in the _existing_ modules that have had a
stable release.  Thus, the only question really is the best way to implement
scope for new modules (and functions).

If you focus on just that case, then I think the best thing to do is to
create a command that locally scopes all variables with some mechanism to
specify the exceptions.  The reason I advocate this general approach is I
feel that most variables will be local with some few well-defined
exceptions, and the fewer variables cmake programmers have to type, the less
chance for scope errors in the programming of _future_ CMake macros and
functions.  I think this method of scoping (local scoping with specific
exceptions) is the method used by most languages (with the exception of perl
as you pointed out) for the very reason that the specific exceptions to
locally scoped variables tend to be a much smaller list of variables than
the list of local variables so maintenance issues and scoping bugs are much
reduced by this approach.

My $0.02.

Alan
__________________________
Alan W. Irwin

Astronomical research affiliation with Department of Physics and Astronomy,
University of Victoria (astrowww.phys.uvic.ca).

Programming affiliations with the FreeEOS equation-of-state implementation
for stellar interiors (freeeos.sf.net); PLplot scientific plotting software
package (plplot.org); the libLASi project (unifont.org/lasi); the Loads of
Linux Links project (loll.sf.net); and the Linux Brochure Project
(lbproject.sf.net).
__________________________

Linux-powered Science
__________________________


More information about the CMake mailing list