lua-users home
lua-l archive

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


Hi Steve Donovan,

I understand what you mean though my understanding is not 100%.

But there is one, YACI[1], I understand that it would be too much complicated and never simple. But it supports something similar to virtual function. It even supports abstract function.

Now I am trying to implement strategy pattern using YACI. I will try it and if I succeeded I will open it here so that people can look and hopely people can give me some feedback. I wish you could also. ^^;

Thank you very much.
Sincerely
Journeyer

[1] http://lua-users.org/wiki/YetAnotherClassImplementation


----------------------------------------
Journeyer J. Joh
o o s a p r o g r a m m e r
a t
g m a i l  d o t  c o m
----------------------------------------


2014-02-25 21:45 GMT+09:00 steve donovan <steve.j.donovan@gmail.com>:
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'.