lua-users home
lua-l archive

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


As per the document(
https://www.lua.org/manual/5.3/manual.html#2.4),
which says that[emphasis mine]:
Tables and full userdata have individual metatables (although
multiple tables and userdata can share their metatables).
Values of all other types share one single metatable per type;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
that is,there is one single metatable for all numbers, one for
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
all strings, etc. By default, a value has no metatable, but the
string library sets a metatable for the string type (see §6.4).

The document clearly states that there is a one single metatable
for all numbers, for all booleans and etc.
Why could not aquire the metatable for the types of LUA_TNUMBER
and LUA_TBOOLEAN(for details, see the test below)?


Here is the aforementioned test which is done on vanilla Lua:
@localhost:~$ lua
Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
> int=1
> double=3.14
> type(int)
number
> type(double)
number
> getmetatable(int)
nil
> getmetatable(double)
nil
> flag=true
> type(flag)
boolean
> getmetatable(flag)
nil
> str=""
> type(str)
string
> getmetatable(str)
table: 0x144ba70
> getmetatable(str).__index
table: 0x144b350
> string
table: 0x144b350
> getmetatable(str).__index == string
true

You see "getmetatable("").__index == string" returns
ture whereas "getmetatable(3.14)" returns nil.