[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: teach your Grandma prototypical inheritance? was (Uncommon OOP in Lua: right or wrong?)
- From: "Patrick Mc(avery" <spell_gooder_now@...>
- Date: Sat, 25 Sep 2010 08:46:24 -0400
Just some silly enthusiast code here but I was wondering if there is
anything terribly wrong with it other then it's global variables?
Would this scheme of prototypical inheritance be suitable for teaching
someone who knew nothing about this topic?
Surely the meta table approach is better but this code seems pretty
easy, perhaps easier for someone new to programming.
Thanks for reading-Patrick
mammal = {}
mammal.blood = function() print("warm") return end
cat = {}
cat.selfDefence = function() print("claws") return end
tabbyCat = {}
tabbyCat.color = 'orange'
function ifFails(field)
local toSort = {
mammal[field],
cat[field],
tabbyCat[field],
}
--manually enter the length of toSort as it has holes
local giveUp = 4
local i = 1
while true do
r = toSort[i]
if i == giveUp then print('no such field found') break end
if r ~= nil then break end
i = i +1
end
return r
end
d = ifFails('blood') --returns a function
e = ifFails("selfDefence") --returns a function
f = ifFails("color") --returns a string
g = ifFails("gonnaFail") --should return nil
d() -->warm
e() --> claws
print(f) -->orange
print(g) -->nil