lua-users home
lua-l archive

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


On Tue, Apr 7, 2015 at 6:14 AM, Sean Conner <sean@conman.org> wrote:
>   Well, not all metamethods are functions.  __mode, __metatable and __name
> (Lua 5.3) are never functions, while __index and __newindex can either be a
> function or a table.  Most, however, are functions [1].

Thank you, I didn't know that all fields of a metatable are called
metamethods. I thounght that only a function can be a metamethod. I
found the proof in Lua manual:

> We call the keys in a metatable events and the values metamethods.

By the way, "closure" was another Lua definition I misunderstood. I
thought that closure is a function instance which uses external local
variables. However this is also wrong. Lua manual says as follows:

> A function definition is an executable expression, whose
> value has type function. When Lua pre-compiles a chunk, all
> its function bodies are pre-compiled too. Then, whenever
> Lua executes the function definition, the function is
> instantiated (or closed). This function instance (or
> closure) is the final value of the expression. Different
> instances of the same function can refer to different
> external local variables and can have different environment
> tables.

So, if I understand correctly, any function instance is called closure.

> [1]     A table I constructed for my own use, and may be useful here:
>
>                5.1     5.2     5.3     function
> __add           *       *       *       a + b
> __sub           *       *       *       a - b
> __mul           *       *       *       a * b
> __div           *       *       *       a / b
> __mod           *       *       *       a % b
> __pow           *       *       *       a ^ b
> __umn           *       *       *       -a
> __idiv                          *       a // b
> __band                          *       a & b
> __bor                           *       a | b
> __bxor                          *       a ~ b
> __bnot                          *       ~a
> __shl                           *       a << b
> __shr                           *       a >> b
> __concat        *       *       *       a .. b
> __len           *       *       *       #a
> __eq            *       *       *       a == b
> __lt            *       *       *       a < b
> __le            *       *       *       a > b
> __index         *       *       *       a = b[]         can be table
> __newindex      *       *       *       a[] = b         can be table
> __call          *       *       *       a()
> __gc            *       *       *
> __mode          *       *       *                       string
> __metatable     *       *       *                       table
> __tostring      *       *       *
> __ipairs                *
> __pairs                 *       *
> __name                          *                       string

Thank you for this nice table. I've converted it into Markdown [1]

[1] https://github.com/starius/helpme/blob/master/lua-metamethods.md

Best regards,
Boris Nagaev