Confusión de usar la notación "." Con __index y espacio de nombres en Lua
Estoy confundido con las siguientes dos sintaxis que usan "."
Por lo que entiendo,__index
se llama cuando una clave no existe en una tabla pero existe en su metatabla. Entonces, ¿por qué llama la tabla de lista__index
y luego asignarse alist.__index
?
list = {}
list.__index = list
setmetatable(list, { __call = function(_, ...)
local t = setmetatable({length = 0}, list)
for _, v in ipairs{...} do t:push(v) end
return t
end })
function list:push(t)
if self.last then
self.last._next = t
t._prev = self.last
self.last = t
else
self.first = t
self.last = t
end
self.length = self.length + 1
end
.
.
.
local l = list({ 2 }, {3}, {4}, { 5 })
HaceWindow.mt
simplemente crear una tabla? Por qué necesitamosWindow = {}
como un espacio de nombres aquí?
Window = {} -- create a namespace
Window.mt = {} -- create a metatable
Window.prototype = {x=0, y=0, width=100, height=100, }
function Window.new (o)
setmetatable(o, Window.mt)
return o
end
Window.mt.__index = function (table, key)
return Window.prototype[key]
end
w = Window.new{x=10, y=20}
print(w.width) --> 100