lua-users home
lua-l archive

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


This is quoted from Lua 5.2 (not 5.3 which is recent) And this is the only text available on official site lua.org (most sites still don't use version 5.3, or the very recent version 5.4 which is not compatible in many aspects).

Version 5.2 really had the implicitly declared local variable "arg", available in the body of every function definition (used to "capture" all function arguments in a pseudo-table, before the addition of "vararg expressions" using the nilary operator "...").

There's still in all current versions the implicitly declared local variable "self", available in the body of every function definition whose name uses the "colon" syntaxic sugar with a "object:" prefix before the given function name.

But it should be noted that you cannot use the "local" declaration
    local function object:method(parameters) body end
NOT equivalent to
    local object.method = function(self, parameters) body end
because the local variable "method" in the second case would not be visible in scope to the function body, and anyway "local object.method" has no sense, the locality of the name for methods is not defined by the current local scope but by the given object in which the method name will be used as a key to store the reference of the function.

And even without this erroneous "local", nothing warranties that the function body will be executed with the same object referenced by this "self" parameter (even if you use the "colon" syntaxic sugar declaring it implicitly... or not and then "self" is not implicitly declared and you can rename it as you want, such as "this", "obj", ...)

The function can still be called without the "method" style, so the function still needs to validate the possible types and values for its first parameter (Lua still does not enforces strict type validation in its declarations, and won't be able to do that... unless functions are annoted by "parser attributes", like what I described in earlier messages (attributes can just be one or more string constants at start of the function body or the start of a lua script, except that they are NOT comments, but they would use a Lua syntax, parsed by the Lua parser in the script loader, these attributes would be conditionally executed by this loader, but then normally invisible to the declared function, except by using some debugging or reflection interface exposed by the loader).

I still think that "parser attributes" are extremely useful, including for security (e.g. distribution of Lua code from external site: this requires validation of digital signatures or certificates that could be embedded easily in these attributes), or code generators (including IDE's that need reliable reflection capabilities to be exposed directly in the source code), or debuggers (that could be written in pure Lua, using their own loader), or for compilation or execution directives (e.g. for setting rounding modes, or allowing to restrict or relax the order of evaluation of operands, operations and function calls).

Supporting "parser attributes" in the Lua syntax is extremely easy: you can add them either at start of function bodies (or start of Lua scripts), or at start of any block (after "do", "repeat", "then", "else") or at start of any other statement (after a required semicolon ";" statement separator), or even at the start of any _expression_ (only after the "=" sign, or between parentheses/brackets/braces just after the leading "(" or "[" or "{") before the actual _expression_. Lua parsers would NOT be required to evaluate all these attributes, they could just discard all of them silently, they would just have to accept the presence of a string constant at these locations. These parser attributes should still use a syntax clearly defined in Lua (probably a comma-separated list of "key", or "key=value", or "key(values)"), and should use naming conventions for its keys, to scope them by use (Lua itself could reserve its own scope, vendors or applications could use conventional dotted naming schemes like those in Java, dotNet, Python... and even already by Lua itself for naming its modules loadable with "require").


Le jeu. 9 sept. 2021 à 11:22, Andrew Gierth <andrew@tao11.riddles.org.uk> a écrit :
>>>>> "Philippe" == Philippe Verdy <verdyp@gmail.com> writes:

 Philippe> See https://www.lua.org/pil/5.2.html.

Stop right there - the website copy of PIL is _NOT_ the documentation,
it is a book about an old version of Lua (5.0) and as such is obsolete
in many respects including this one.