lua-users home
lua-l archive

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


On 2013-05-16 7:19 PM, "Tim Hill" <drtimhill@gmail.com> wrote:
>
>
> >
> > 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.
>
> --Tim
>
>

Lately I've been wishing for a simple "interface" functionality in Lua, where I could say "anything implementing 'foo' must have methods x, y and z" and perhaps provide default methods as well. But I haven't been able to come up with a syntax that isn't awkward and doesn't add overhead to every instantiation. For that matter since Lua is interpreted, any kind of interface system would add overhead to at least every time the module is loaded, which might be an issue for e.g. a web server; I wonder if there's a way to eliminate that overhead using luac...