Reputation: 15756
I have a module called "MyClass.lua" It contains this:
local _S = {}
function _S:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
return _S
In my main script, I refer to the module like this:
local c = require "MyClass"
local t = c.new()
This results in an error. It's complaining about setmetatable
.
<snip>\MyClass.lua:6: bad argument #2 to 'setmetatable' (nil or table expected)
I think I'm confused about what setmetatable
does, the scope of self
, and the role of __index
.
Upvotes: 1
Views: 453
Reputation: 122493
The self
parameter is only implicit when using the OOP style.. You need to call it like this as well:
local t = c:new()
-- ^
Upvotes: 3