-
using
This should add a new function
using
Is this possible at all and if so how to do this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
in luaaa, exported class is userdata instead of table: print("type of AwesomeCat:", type(AwesomeCat.new("CCC"))) output:
we can wrapper SpecialCat = {}
function SpecialCat:new(name)
o = AwesomeCat:new(name)
setmetatable(self, getmetatable(o))
return self
end but there is an issue in luaaa codes, luaaa always expect an userdata from stack, so we had to store origin userdata in table, and read it from luaaa by specified name, we use '@' as it's name: function SpecialCat:new(name)
o = AwesomeCat:new(name)
setmetatable(self, getmetatable(o))
self['@'] = o
return self
end now wen can extend SpecialCat and override it's base class's method: function SpecialCat:onlyInSpecial()
print(self:getName() .. " has a special cat function")
end
function SpecialCat:speak(text)
print("Special cat " .. self:getName() .. " says: " .. text)
-- and base method can be call as below:
self["@"]:speak(text)
end rearrange above codes, we got below: -- put utility funtions to name scope 'luaaa'
luaaa = {}
-- create subclass for base, obj can be exist table or nil
function luaaa:extend(base, obj)
derived = obj or {}
derived.new = function(self, ...)
o = base.new(...)
setmetatable(self, getmetatable(o))
self["@"] = o
return self
end
return derived
end
-- get base class of obj
function luaaa:base(obj)
if (type(obj) == "table") then
return obj["@"]
end
return nil
end
by with above utility functions, you can extend lua class likes below: SpecialCat = luaaa:extend(AwesomeCat, {value = 1})
-- or:
-- SpecialCat = luaaa:extend(AwesomeCat)
-- in this case there no attribute for extended new class
function SpecialCat:onlyInSpecial()
print(self:getName() .. " has a special cat function")
print("Special cat " .. self:getName() .." has value:" .. self.value)
end
function SpecialCat:speak(text)
print("Special cat[" .. self:getName() .. "] says: " .. text)
-- call override base method:
luaaa:base(self):speak(text)
end use SpecialCat: xxx = SpecialCat:new("xxx")
xxx:speak("I am a special cat.")
xxx:onlyInSpecial() BTW: |
Beta Was this translation helpful? Give feedback.
-
please pull the latest version(>= 0.2.3), then add below utility functions to your project . -- put utility funtions to name scope 'luaaa'
luaaa = {}
-- create subclass for base, obj can be exist table or nil
function luaaa:extend(base, obj)
derived = obj or {}
derived.new = function(self, ...)
-- NOTICE: in next major version, below line will be changed to 'o = base:new(...)'
o = base.new(...)
setmetatable(self, getmetatable(o))
self["@"] = o
return self
end
return derived
end
-- get base class of obj
function luaaa:base(obj)
if (type(obj) == "table") then
return obj["@"]
end
return nil
end |
Beta Was this translation helpful? Give feedback.
in luaaa, exported class is userdata instead of table:
output:
we can wrapper
userdata
totable
to make inheritance works, likes below:but there is an issue in luaaa codes, luaaa always expect an userdata from stack, so we had to store origin userdata in table, and read it from luaaa by specified name, we use '@' as it's name:
now wen can extend Specia…