lua-users home
lua-l archive

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


2016-05-13 11:56 GMT+02:00 Viacheslav Usov <via.usov@gmail.com>:
> On Thu, May 12, 2016 at 9:51 PM, Nagaev Boris <bnagaev@gmail.com> wrote:
>
>> By the way, without any globals, how can one use normal globals like
>> "print", "math" etc?
>
> As mentioned by others, you would need to reference them explicitly via
> _ENV. So, _ENV.print, etc.
>
>> Always declaring them is annoying.
>
> It is already standard practice in Lua to have local references to globals:
>
> local print, random = print, math.random
>
> So you would just have to say
>
> local print, random = _ENV.print, _ENV.math.random
>
> I do not think that is that big a burden, and the benefit - immunity to
> typos - is huge.

We have come a long way from the original question, which was:

> What speaks against implementing a new Lua compilation mode
> where everything must be explicitly declared to be either local or
> global, and anything not so declared would result in a compilation
> error?

Some answers to this question have actually been given.
In an earlier message, concern was expressed for Lua script
writers who are not programmers by trade.

1. We will need some new syntax.
2. An explicit _ENV is needed to refer to predefined globals.
3. The major stumbling block is how to declare globals.
4. When Lua is used as a configuration language, the need
to predefine globals is intrusive.
5. This would be a way to impose policy, which is supposed
to be something Lua does not do.

The time has perhaps come to reverse the question.

> What speaks in favour of implementing a new Lua compilation
> mode despite all that speaks against it?

Here, too, some reasons have been given.

1. It wll benefit Lua script writers who are not programmers by trade.
2. It will confer immunity against typos.

One could add:

3. It guards against failure to declare as local something that should
have been.

The fact remains that any attempt to make globals explicit at compile
time means that that another name class is being added to Lua.

Now: reserved words, locals, upvalues, globals by default.
Then: reserved words, locals, upvalues, globals, undefined by default.

I have put globals after upvalues, but given the inconvenience
involved in declaring them, they should be more important. I.e.

reserved words, globals, locals, upvalues, undefined by default.

I.e. it should be an error to declare as local a name that has
already been declared as global.