p. languages: Lua
August 23rd, 2007I am no experienced Lua-er. But I got very intrigued after the first “aha” moment happened to me: “aha, so lua is like some super-table language?”. That is of course not all, and many more aha-s did and will follow.
I love minimalism and this is a minimalism at it’s best!
Just some random code to confuse you:
function makeHiSayer(name)
return function()
function sayHi(name1)
print("hi, I am " .. name1)
end
local n = name
sayHi(n)
end
end
hi = makeHiSayer("janko")
hi()
-- hi, I am janko
from Wikipedia: Although Lua does not have a built-in concept of classes, the language is powerful enough to easily implement them using two language features: first-class functions and tables. By simply placing functions and related data into a table, an object is formed. Inheritance (both single and multiple) can be implemented via the “metatable” mechanism, telling the object to lookup nonexistent methods and fields in parent object(s).
In next example I am not using any of the proposed methods for doing OO in Lua. Basically I am just making stuff up as I go and yet it works.
AmIABulletClass = { x = 10, y = 20, dx = 1, dy = 1,
update = function (self)
self.x = self.x + self.dx ;
self.y = self.y + self.dy
end }
bul = AmIABulletClass
bul.update(bul)
print(bul["x"])
-- 11
print(bul["y"])
-- 21
bul.update(bul)
print(bul.x)
-- 12
print(bul.y)
-- 22
