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)


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)


[Please log in to post a comment]