[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: How am I supposed to use yaci library?(Yet Another Class Implementation)
- From: steve donovan <steve.j.donovan@...>
- Date: Tue, 25 Feb 2014 14:45:22 +0200
On Tue, Feb 25, 2014 at 5:21 AM, Journeyer J. Joh
> 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'.