creating a new module as an extention of another module

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

creating a new module as an extention of another module

david herviou
Hi all,
I'm now using lua 5.1 and migrating my previous package system ('hack'
describes in pil 5.0) to this lua version.
I think it's now very clean with this new package module system but I
have a question on how to do a module
that is an extension of an another module?
for example
  --file : rule.lua
    module("rule")
    function and(r1,r2) ... end
    function or(r1,r2) ... end

suppose now you want to extend the previous module with to have a
CompleteRule system
    --file completeRule.lua
    module("completeRule")
    require "rule"
   
    function notUnary(r1) ... end

the question is finally how to push all function of module rule into the
module completeRule. In a way, it's like inheritance for class.
I think I have already seen such mecanism in Ocaml.
Someone as an idea? or is it to basic to ask the question (I'm a newbie
concerning the packaging system)?

Thanks.
dh


herviou.vcf (590 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: creating a new module as an extention of another module

Mildred Ki'Lya

you can try :

-----------------------
local _G = _G
module("completeRule", function(m)
        getmetatable(m).__index = require("rule")
        return m
end)

function additionalRule(something)
        return _G.print(something)
end
------------------------

or :

-----------------------
module("completeRule", package.seeall, function(m)
        for k, v in require("rule") do
                m[k] = v
        end
        return m
end)

function additionalRule(something)
        return print(something)
end
------------------------


--
Mildred       <xmpp:[hidden email]> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]
Reply | Threaded
Open this post in threaded view
|

Re: creating a new module as an extention of another module

Petite Abeille
In reply to this post by david herviou

On Mar 31, 2006, at 17:27, david herviou wrote:

> Someone as an idea? or is it to basic to ask the question (I'm a
> newbie concerning the packaging system)?

I guess we all are, as it's brand new :)

Tomas posted some code recently showing how it could be done:

>         I would like to add that if you want some inheritance, you
> could
> use the second argument of `module()' to achieve an elegant solution.
> local obj = require"obj"
> module ("myobj", obj:inherit())

http://lua-users.org/lists/lua-l/2006-03/msg00336.html

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/

Reply | Threaded
Open this post in threaded view
|

Re: creating a new module as an extention of another module

Petite Abeille
In reply to this post by david herviou

On Mar 31, 2006, at 17:27, david herviou wrote:

> I think it's now very clean with this new package module system but I
> have a question on how to do a module
> that is an extension of an another module?

Here is another variation on module and inheritance:

function extends( aSuper )
         if type( aSuper ) == "string" then
                 aSuper = require( aSuper )
         end

         aSuper = aSuper or { __index = _G }

         return function( aClass )
                 setmetatable( aClass, aSuper )
                 aClass.__index = aClass

                 aClass.self = aClass
                 aClass.super = aSuper

                 return aClass
         end
end

-- define the root class
module( "Object", extends() )

function self:doIt()
         return "do it"
end

-- define a subclass
module( "MyObject", extends( "Object" ) )

function self:doIt()
         return "about to " .. super.doIt( self )
end

-- define another subclass
module( "MyOtherObject", extends( "MyObject" ) )

function self:doIt()
         return super.doIt( self ) .. " well"
end

print( self:doIt() )

 > about to do it well

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/