Access to Lua Meta Tags

Obviously, getmetatable can access several types of meta tags:

getmetatable("") getmetatable({}) getmetatable(newproxy(true)) 

However, it seems that you cannot get the meta from other types (functions aside). There seems to be no way to access metatable numbers, booleans or nil.

I was also wondering if it is possible to access the metatable of the entire table type. To do something like this:

 ({}) + ({}) 
+2
source share
2 answers

Numbers, Booleans, and nil do not have metadata by default (hence getmetatable return nil ). You can give them one with debug.setmetatable , though.

There is no general table that can be metathema. (and the same for userdata (at least from a wide variety))

0
source

strings, numbers, zero, functions, and lightuserdata have one metatable for the whole type. Tables and full user data have metadata for each instance.

from documents:

Tables and full user data have separate meta tags (although multiple tables and user data can share their meta tags). Values โ€‹โ€‹of all other types share one single metatet type; i.e. one metatable for all numbers, one for all strings, etc., etc.

there is no 'metatable table type', just as there is no 'metatable for this row'

the row type has the table "string" as metatetable by default; but you can set the meta for other types using debug.setmetatable() function.strings, etc.

+5
source

Source: https://habr.com/ru/post/1415163/


All Articles