lua-users home
lua-l archive

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


On Sunday 12 February 2006 00:41, Gavin Kistner wrote:
[...]
> I'm confused what you mean by this. Could you elaborate?

It's the standard Python trick (which suffers from this same problem). When 
you define a class, you define symbol local to the class that points at your 
class' superclass (does that sentence actually make sense)? It means that you 
can use clearer terminology when you call methods, and if you want to change 
your class hierarchy you only need to modify one thing rather than hunting 
down every place where you call your superclass.

The above code is an extension of this concept where PA's defining a symbol 
that points at the *current* class. Interesting idea, and possibly one I'll 
think about adopting.

...

The whole call-your-superclass thing is pretty horrible to implement in a 
language like Lua, because it looks deceptively like an ordinary method 
dispatch. It's not. It uses a different method search algorithm (one that 
skips the object's immediate class and moves directly on to its superclass). 
Prototype-based languages like Lua and Javascript and, to a lesser extent, 
Python can't really cope with this because most object implementations 
flatten out the class structure when they create an object --- all methods 
belonging to all of the object's superclasses end up in one big table. This 
simplifies and speeds up method dispatch no end, but it does mean you've lost 
all the information you need to do super dispatch.

If you were willing to implement your object system using true inheritence --- 
where calling __index__ on your object attempts to look up the method on your 
object's class, and if that fails it finds the superclass and calls __index__ 
on that --- you could do it. But in a language like Lua, nobody wants to do 
anything like that because it's inefficient.

Possibly a best-of-both worlds approach might be useful; copy all methods into 
a single dispatch table, but *also* keep a chain of tables pointing at the 
object's superclasses... it would make super dispatch quite a lot slower than 
method dispatch, though.

-- 
+- David Given --McQ-+ (base 10) = (base pi)
|  dg@cowlark.com    | 1 = 1; 2 = 2; 3 = 3
| (dg@tao-group.com) | 3.1415926... = 10.0
+- www.cowlark.com --+ 4 = 12.201220211...

Attachment: pgpEDoMpir2dv.pgp
Description: PGP signature