lua-users home
lua-l archive

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


Op Do. 18 Apr. 2019 om 21:16 het Peter Aronoff <telemachus@arpinum.org> geskryf:

> Thanks for asking this question, Hisham! I was too shy, but I had the
> exact same thought. A few years ago, I rewrote all my modules *not* to use
> _VERSION, _AUTHOR, etc.[1]
>
> Maybe I didn’t need to rewrite them after all?

I've always thought about reserved names in a Lua 5.1 sort of way:
such global variables are reserved, but in one's own tables it's OK.
But you have now forced me to RTFM. I'm jumping over 5.2 because it
does not seem to have the same retro appeal that 5.1 has.

Under "Lexical conventions"
5.1: As a convention, names starting with an underscore followed by
uppercase letters (such as _VERSION) are reserved for internal global
variables used by Lua.
5.3: As a convention, programs should avoid creating names that start
with an underscore followed by one or more uppercase letters (such as
_VERSION).

Under "Registry"
5.1 and 5.3: Any C library can store data into this table, but it
should take care to choose keys different from those used by other
libraries, to avoid collisions.
5.3: As with variable names, string keys starting with an underscore
followed by uppercase letters are reserved for Lua.

The letter of the law is now "avoid creating names that start with an
underscore followed by one or more uppercase letters".

But is that the spirit of the law? The mere fact that the registry has
been singled out in Lua 5.3 seems to imply that the issue is less
crucial in other tables than in the registry and the global
environment.

Ah, but in Lua 5.3 any table could in principle be the value _ENV. So
by the letter of the law it's no longer "internal global variables"
only, it's "names" in general.

Does "reserved for Lua" mean "never change this value"? I think not.
If so, the variable could have been made read-only. I think it means
"use it in the way that Lua uses it". By that logic, _VERSION in a
table, and particularly a table that is used as a library, should give
version information about that table.

Do we have precedents? No. Roberto (in `lpeg`) and Luiz (in `mathx`
etc) use a plain lowercase "version". (For a string, though, not a
function as Peter has it.)

But would it not be a useful convention?