lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


on 2/27/05 2:46 AM, crow at crow_64a@blueyonder.co.uk wrote:

> If all variables were local to the point where they are declared, and to
> things within that code in which they are declared, we could avoid the word
> local by putting them in the right places and being careful with names. Is
> there some reason this would not work?

What is the "point where they are declared" if one doesn't have
declarations? If it's the point where the variable is first mentioned, then
it becomes difficult to create a variable without assigning to it. It also
means that outer changes can break inner code by introducing a new variable
name.

So, local by default doesn't actually make as much sense as one might think.
One could have it mean "local to the chunk" but that's actually easy to get
by sandboxing the chunk in an environment that derives from the global
environment.

For non-interactive code, what is probably more interesting is requiring
that all variables be declared.

But I would probably be happy with a way to hook the compiler so that it
would call an error check function when encountering reads and writes to
global variables. This would enable checks at compile time instead of
runtime. Actually, a general hook mechanism might look something like the
following:

The compiler receives an optional function taking two parameters a token and
a usage and returning either nil or an error message. This would be passed
information for all global variable reads and writes, all field reads and
writes (dot notation), and all method calls.

This probably needs to be complemented with some form of declaration
mechanism/hook, but I don't have as clear a proposal for that.

Mark