lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Whoops, forgot to actually show the inheritance lookup being used.
Let's try that again, now that I've had my coffee...

[====[
local function inherit(x, y) setmetatable(x, {__index=y}) end

mammal = {}
mammal.blood = function() return "warm" end
mammal.selfDefense = function() error("override me") end

cat = {}
inherit(cat, mammal)
cat.selfDefense = function() return "claws" end

dog = {}
inherit(dog, mammal)
dog.selfDefense = function() return "bite" end

-- doesn't override anything, just shows that missing fields are shared
aNormalCat = {}
inherit(aNormalCat, cat)

ninjaCat = {}
inherit(ninjaCat, cat)
ninjaCat.selfDefense = function() return "shuriken" end

print("cat -> ", cat.selfDefense())
print("dog -> ", dog.selfDefense())
print("aNormalCat -> ", aNormalCat.selfDefense())  --same as cat
print("ninjaCat -> ", ninjaCat.selfDefense())
]====]

Scott