Considering the following code:
http.lua
local function create_socket()
-- do stuff to create socket
return socket, err
end
local _M
local mt = { __index = _M }
function _M:new()
local sock, err = create_socket()
if not sock then
return nil, err
end
return setmetatable({ sock = sock, keepalive = true }, mt)
end
function _M:function1() end
function _M:function2() end
return _M
http_extender.lua
local http = require "http"
local _M = {}
function _M:new()
local o = setmetatable(self, {__index = http.new()})
return setmetatable({}, {__index = o})
end
function _M:function3() end
function _M:function4() end
return _M
Given that http_extender is meant to be a module that extends http's functionality, a couple of questions:
- I have the feeling that there is something inherently wrong with
http_extender:new()because it is modifyingselffor each call, right? - If indeed wrong, what would be the correct way to do it so that each call to
http_extender:new()creates a newhttp"object" that is composed with the functionalities and attributes ofhttp_extender?
Thanks