Log In  

Hi,
I would like to do oop... but I need setmetatable and self.__index.
Please, can you add them?
Regards,
jihem

p={}
function p:new(o)
  o=o or {}
  setmetatable(o,self)
  self.__index=self
  self.x=o.x or 0
  return o
end
function p:add(n)
  self.x=self.x+n
end
q=p:new({x=10})
q:add(5)
print(q.x)
P#18393 2016-01-22 05:38 ( Edited 2016-01-22 10:55)

Just found out how to get this done...
This is enougth for my actual need.
Cheers :-)

--
function setmetatable(t,m)
    local k,v
    t=t or {}
    for k,v in pairs(m) do
     t[k]=v
    end
    return t
end
--
p={}
function p:new(o)
    self=setmetatable(o,self)
    self.x=o.x or 0
    return self
end
function p:add(n)
  self.x=self.x+n
end
q=p:new({x=10})
q:add(5)
print(q.x)
P#18394 2016-01-22 05:55 ( Edited 2016-01-22 10:56)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-18 22:02:28 | 0.006s | Q:11