The index metamethod can be set equal to tables. From what I can tell
foo.__index = function(self, k)
return bar[k]
end
and
foo.__index = bar
are the same. Why is declaring functions this way allowed in this situation?
The index metamethod can be set equal to tables. From what I can tell
foo.__index = function(self, k)
return bar[k]
end
and
foo.__index = bar
are the same. Why is declaring functions this way allowed in this situation?
Copyright © 2021 Jogjafile Inc.
This isn't a function declaration - assigning a table to
__index
is just a shortcut for using the function that you described.From Programming in Lua (for Lua 5.0, but this part of the language hasn't changed):
It's not like the table that you assign magically becomes a function.
type(foo.__index)
will still returntable
, and you can still do things with it that you can do with other tables, like usingpairs
andnext
, etc.