inheritance example

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

inheritance example

Steve Elkins
May I have an inheritance example please?

http://lua-users.org/wiki/LuaClassesWithMetatable has everything I want
except that and I can't see how to do it.

Thanks,
Steve


Reply | Threaded
Open this post in threaded view
|

Re: inheritance example

RLak-2
> May I have an inheritance example please?

base = {}

function base._new(class, words)
  return setmetatable(words, {__index = class})
end

function base:new(...)
  return base._new(self, arg)
end
 
function base:punctuate()
  return table.concat(self, self.sep)
end

function base:stream()
  return table.concat(self, " ")
end

base.sep = ", "

----

dotty = base:new()
dotty.sep = ". "

---


-------- Sample:

U:\>lua
Lua 5.0  Copyright (C) 1994-2003 Tecgraf, PUC-Rio
> obj1 = base:new("Hello", "World")
> obj2 = dotty:new("Hello", "World")
> =obj1:punctuate()
Hello, World
> =obj2:punctuate()
Hello. World
> =obj1:stream()
Hello World
> =obj2:stream()
Hello World
>

------------

Some may object to the style; but it has served me reasonably well.

Note that a lot of identical metatables are created; memoising their 
creation would probably be a good idea.

Rici.


Reply | Threaded
Open this post in threaded view
|

Re: inheritance example

RLak-2
I should have provided this link: it is very interesting reading. Self is 
still very much alive but I don't know of any Wintel implementations. Sad. 
really.

<http://research.sun.com/self/papers/organizing-programs.html>



Reply | Threaded
Open this post in threaded view
|

Re: inheritance example

Steve Elkins
In reply to this post by RLak-2
On Wed, 16 Jul 2003 17:22:36 -0500
[hidden email] wrote:

[...something other than what I wanted because I asked so unclearly...]

Given the code from the page in the comment...

  -- http://lua-users.org/wiki/LuaClassesWithMetatable

  function Class(members)
     members = members or {}
     local mt = {
        __metatable = members;
        __index     = members;
     }
     local function new(_, init)
        return setmetatable(init or {}, mt)
     end
     local function copy(obj, ...)
        local newobj = obj:new(unpack(arg))
        for n,v in pairs(obj) do newobj[n] = v end
        return newobj
     end
     members.new  = members.new  or new
     members.copy = members.copy or copy
     return mt
  end

...how can it be extended so that classes can have parents.  If it's
already there, I don't see it.

Thanks,
Steve


Reply | Threaded
Open this post in threaded view
|

Re: inheritance example

Philipp Janda
Am 17.07.03 14:52 schröbte Steve Elkins:
On Wed, 16 Jul 2003 17:22:36 -0500
[hidden email] wrote:


I think, the following should work...


    function Class( members, parentclass )

     members = members or {}
     local mt = {
        __metatable = members;
        __index     = members;
     }
     local function new(_, init)
        return setmetatable(init or {}, mt)
     end
     local function copy(obj, ...)
        local newobj = obj:new(unpack(arg))
        for n,v in pairs(obj) do newobj[n] = v end
        return newobj
     end
     members.new  = members.new  or new
     members.copy = members.copy or copy

       if parentclass then
         local pmt = {
           __metatable = parentclass,
           __index     = parentclass
         }
         setmetatable( members, pmt )
       end

     return mt
  end

...how can it be extended so that classes can have parents.  If it's
already there, I don't see it.

Thanks,
Steve


Philipp



Reply | Threaded
Open this post in threaded view
|

Re: inheritance example

Philipp Janda
Am 17.07.03 16:56 schröbte Philipp Janda:

I think, the following should work...

Or this for multiple inheritance (untested!)...



      function Class( members, ... )

     members = members or {}
     local mt = {
        __metatable = members;
        __index     = members;
     }
     local function new(_, init)
        return setmetatable(init or {}, mt)
     end
     local function copy(obj, ...)
        local newobj = obj:new(unpack(arg))
        for n,v in pairs(obj) do newobj[n] = v end
        return newobj
     end
     members.new  = members.new  or new
     members.copy = members.copy or copy

        local pmt = {
          __index = function( _, key )
            for i=1,arg.n do
              local parentmethod = arg[i][key]
              if parentmethod then
                return parentmethod
              end
            end
          end
        }
        setmetatable( members, pmt )

     return mt
  end

...how can it be extended so that classes can have parents.  If it's
already there, I don't see it.

Thanks,
Steve



Philipp






Reply | Threaded
Open this post in threaded view
|

Re: inheritance example

Steve Elkins
In reply to this post by Philipp Janda
On Thu, 17 Jul 2003 16:56:58 +0200
Philipp Janda <[hidden email]> wrote:

> I think, the following should work...

[..snip..]

It does, thank you very much.