lua-users home
lua-l archive

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

On Mon, 2005-01-17 at 04:07, André de Leiradella wrote:

> The thing the bothers me most is how to call inherited methods.

An object is nothing more than a table of closures. 
OO is just a minor subset of functional programming.

Inheritance is no problem, just modify the table
in the derived class .. which is just a function
that calls the base function first to
make the initial table.

function base(x) 
  return { 
    ["meth1"] = function () return x end;
    ["meth2"] = function () return 2 end

function derived (x)
  methods = base(x)
  function meth1 () return 3 end
  methods.meth1 = meth1
  return methods

d = derived(x)
d.meth1() -- calls overriden meth1
d.meth2() -- calls base meth2

Note -- no 'self' argument is needed,
the private variables of a class can be
refered to directly in the method without
any 'self'. Also no metatables or environments
(whatever they are) are needed. 

All you need is lexically scoped functions and tables,
Lua already has both.

You can even toy with this:

function metaDerived (base)
  methods = base()

where you pass the base constructor in .. try doing *that*
in Java or C++. Or you can pass the table in:

function anotherDerived (basetab)
  function meth1 = ..
  basetab.meth1 = meth1
  return basetab

You can even make 'meta-classes' which are nothing
more than curriable functions, that is, functions
that return functions. In other words, your meta-class
is a class creating a class which creates an object,
which is just a function which returns the class 
constructor function .. :)

So really, you just don't need OO when you have FP.

John Skaller,
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language