lua-users home
lua-l archive

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

> Because an abstract class is basically a type definition, and because lua is a dynamic language with no facilities for defining types, I think you're unlikely to find many OOP libraries supporting abstract methods. 

Really? I agree generally with your main points, but I see no barrier to doing type abstraction in Lua if you REALLY need it (which, I feel, you normally don't .. most people who ask for this are, I suspect, doing so because they've been taught to do that in other languages).

The difference in Lua, of course, is that values carry type information, nor variables, so the type system must be dynamic (run time), not static. For example:

type1 = { f1 = "number", f2 = "boolean", f3 = "string" }

There … a type. At this point an "istype(…)" function is pretty easy to write, all you do is check the fields of a table against the types specified by the above table, which is then a template or interface or whatever you want to call it. This is pretty easy to do for variadic functions also (though a bit harder for fixed functions), and thus can also be applied to methods.

Not saying this is elegant, or even good .. I would probably never use it, but it IS possible.