4

I noticed a lua module setting the returned table's __index as itself

local M = {
  _VERSION = "1.0.0"
}
M.__index = M

function M.do()
end

return M

What does setting a table's __index as itself accomplish?

Later, you would use the module

local m = require("m")
m.do()
joeforker
  • 40,459
  • 37
  • 151
  • 246

1 Answers1

3

It is usually done to avoid creating a separate metatable to be used in objects created by the library:

function M.new()
    return setmetatable({},M)
end

I do this all the time in my libraries. It is somewhat lazy.

lhf
  • 70,581
  • 9
  • 108
  • 149