[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: scoping rules
- From: rw20@...
- Date: Fri, 11 Jun 1999 08:28:04 -0400 (EDT)
Thanks for the good points. I think most of us had missed the technique
of setting the nil tag methods. Another thing that some people might
like to add to your code to control lua globals is an effective 'global'
keyword so that one can not create globals without declaring them first.
To do this one could add a global("name") function that would store
allowed global names to a table and then have the setglobal function
check that table when it creates variables (the check only happens on
global creation, so there isn't really a speed penalty).
Amazing language. Reminds me of all the cool things you can do in Forth
or Scheme, but with better structure than Forth and easier syntax than
On Thu, 10 Jun 1999, Roberto Ierusalimschy wrote:
> In Lua you do not need global declarations. You can complain about this
> (and many people do); but to assume that a variable without this global
> declaration should be automatically considered local does not look "normal"
> to most languages, and certanly is not the right way to solve the problem
> with undeclared global variables.
> If the real problem is the use of global variables without declaration,
> there is an easy and efficient way to solve that, using tag methods. The
> idea is to set the tag methods 'getglobal' and 'setglobal' over the tag nil
> (which is the value of any "undeclared" variable), and to keep a table with
> the name of all declared global variables. When you read or write a nil
> global variable, the tag method goes to the table to check whether the
> variable has been declared. When you read or write a non-nil global, the
> most frequent case, there is no cost at all.