Lua recently became very popular. I encountered Lua scripting in several applications including popular games, I believe it’s used in even more then Wikipedia says.
It’s extremely simple:
- 6 data types (that’s counting “nul” and “function”),
- no C++-style OOP out-of-box (but you can program one, he-he),
- 400K of pure-C code
but is loaded with in-fashion features like
- first-class functions,
- closures,
- coroutines…
- again, Wikipedia is the best.
The only metaprogramming tutorial (with a ready code to implement virtual methods) is “Programming in Lua” book. Still, I don’t see a code to implement ad-hoc polymorphism… Maybe it’s a reason for another post 🙂
Redistributed book removed: it violated copyright.
2 Comments
You can implement polymorphism through creative use of setmetatable and __index. Look through the Lua Wiki, the Lua Book or some of code from the Warcraft WowAce project for examples.
On the second thought, ad-hoc polymorphism is easily implemented in Python-style constructor, just by calling parent constructor and reassigning proper methods.
OTOH, I could be thinking about parametric polymorphism when I was writing “ad-hoc”. Which really needs a metatable trick with __call override and runtime parameter type analysis. And still a topic for another interesting hack 🙂
Post a Comment