lua-users home
lua-l archive

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


On Tue, Feb 25, 2014 at 5:21 AM, Journeyer J. Joh
<oosaprogrammer@gmail.com> wrote:
> Does penligh's class library support virtual function?

As Andrew says, the concept does not apply to Lua.

It is useful to look at the basics of 'OOP' in Lua:

obj:method(args) is exactly equivalent to obj.method(obj,args) - so
'method lookup' is exactly the same as table lookup.  So the simplest
possible scheme is to put the functions inside obj, which is wasteful
for many objects - they all have to contain references to the
functions.  If  obj has a metatable M, and M.__index = T, then if
looking up 'method' fails in obj, it will look in T.  This allows us
to keep only the state in the object, and the functions elsewhere.

Easiest way of seeing inheritance is the 'fat metable', where T will
initially contain all the inherited functions ('copy of base class'),
and then the new 'overrides' will be copied into T.  To a first
approximation you can think of T as a VMT (Virtual Method Table)
except it is indexed by name, not slot.

So everything is 'virtual', because obj.method will always pick up the
version redefined for that 'class'.