inheritence and base element syntax cookie ?

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

inheritence and base element syntax cookie ?

Grellier, Thierry

-- Hello,

 

-- Lua equivalent to C++:

 

-- #include <iostream>

-- struct C { void m() { std::cout << "C::m()" << std::endl; } };

-- struct D : C {

--   void m() {

--     C::m(); // base class

--     std::cout << "D::m()" << std::endl;

--   }

-- };

-- int main() { D d; d.m(); return 0; }

 

C = { }

 

function C:new( o )

  o = o or { }

  setmetatable(o, self)

  self.__index = self

  return o

end

 

function C:m() print("C:m()") end

 

D = C:new{}

 

function D:m()

  C.m( self )    -- no better OO-like syntax available ? like :C:m() for example ?

  print("D:m()") -– I’d like to clearly express this is different from call to external function

end

 

 

Reply | Threaded
Open this post in threaded view
|

Re: inheritence and base element syntax cookie ?

David Given
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Grellier, Thierry wrote:
[...]
> function C:new( o )
[...]
> function D:m()
>
>   C.m( self )    -- no better OO-like syntax available ? like :C:m() for
> example ?
[...]

It very much depends on your OO system. Most Lua OO systems don't
support any kind of 'super' syntax, because when an object is created
they typically copy all methods from all superclasses into a single
table in order to speed up method dispatch.

Your OO system appears to be trying to maintain the superclass chain, as
far as I can tell --- this will work, and will allow you to do
superclass calls, but will make method dispatch much slower (because the
system will need to do a table lookup at every level in the class
hierarchy). I also suspect there's a bug in your C:new() function;
"self.__index = self" isn't going to do anything terribly useful.

One of the drawbacks of Lua's roll-your-own OO system is that it's a bit
hard to get your head around at first.

The OO system I tend to use is based on the following function:

- ---snip---
function Class(...)
        local c = {}

        for _, base in ipairs(arg) do
                for i, j in pairs(base) do
                        c[i] = j
                end
        end

        c.__index = c
        function c.new(...)
                local o = {}
                setmetatable(o, c)
                o:_init(unpack(arg))
                return o
        end

        return c
end
- ---snip---

Calling Class() creates a class, subclassing zero or more other classes
if required. This would make your code:

- --snip---
C = Class()
function C:m()
    print "C:m()"
end

D = Class(C)
function D:m()
    C.m(self) -- no syntactic sugar here
    print "D:m()"
end

d = D.new() -- note use of . and not :
d:m()
- ---snip--

(Untested. There may be bugs.)

One day I'm going to make a page in the Wiki collecting all the various
OO systems people have come up with and comparing them...

- --
+- David Given --McQ-+
|  [hidden email]    | "Those that repeat truisms, are also forced to
| ([hidden email]) | repeat them." --- Anonymous from Slashdot
+- www.cowlark.com --+
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFESLAuf9E0noFvlzgRAo/IAKDfRzr8KnqzuuGCPspLkCsySJUdkwCaA3L8
rTLQnTvMNZsp++5e/bvT7Zc=
=Fn13
-----END PGP SIGNATURE-----
Reply | Threaded
Open this post in threaded view
|

Re: inheritence and base element syntax cookie ?

Daniel Silverstone
On Fri, 2006-04-21 at 11:13 +0100, David Given wrote:
> It very much depends on your OO system. Most Lua OO systems don't
> support any kind of 'super' syntax, because when an object is created
> they typically copy all methods from all superclasses into a single
> table in order to speed up method dispatch.

Mine rolls them up, but still supports a 'super' syntax, but it does so
with lots of closures and temporary tables.

Essentially each instance is a tuple of the class it is, the class this
instance acts as and the instance variables.
When a method is dispatched, 'self' is (K,K,I) but when parent(self) is
called, you get (K,parent(K),I) back. So parent(self):method() calls
'method' in parent(K) passing (K,K,I) in, such that self:overridden() is
the overridden version not the version in parent(K)

I'm not being terribly clear here but that's essentially what goes on.

You can find my class implementation at
http://bzr.digital-scurf.org/trees/dsilvers/aranha/lua/startup/10-Classes but that might be out of date and checking aranha out of bzr with:

bzr get http://bzr.digital-scurf.org/trees/managed/aranha

might be a better plan.

My class system does rely on aranha's version of lua though so it will
need (trivial) changes before it could run on an unpatched lua
interpreter.

D.

--
Daniel Silverstone                         http://www.digital-scurf.org/
PGP mail accepted and encouraged.            Key Id: 2BC8 4016 2068 7895


Reply | Threaded
Open this post in threaded view
|

Re: inheritence and base element syntax cookie ?

Petite Abeille
In reply to this post by Grellier, Thierry
On 4/21/06, Grellier, Thierry <[hidden email]> wrote:
> function D:m()
>
>   C.m( self )    -- no better OO-like syntax available ? like :C:m() for
> example ?
>
>   print("D:m()") -– I'd like to clearly express this is different from call
> to external function
>

Perhaps this is simple wording issue...

Assuming a class 'List' extending 'Object':

-- define the class
local super = Object
local self = super()
       
-- method for initialization
function self:init( aContent, aMode )
    self = super.init( self )
 
    -- do subclass stuff
       
    return self
end

'self' and 'super' are sufficiently OO for any practical purpose.
Additionally, 'super' is properly scoped as well.

http://dev.alt.textdrive.com/browser/lu/LUList.lua#L23
http://dev.alt.textdrive.com/browser/lu/LUObject.lua#L57

--
Cheers

--
PA
http://alt.textdrive.com/
Reply | Threaded
Open this post in threaded view
|

Re: inheritence and base element syntax cookie ?

Asko Kauppi
In reply to this post by David Given

This would be very, very nice!  Please do. :)

I am still puzzled by this area, after 4 years with Lua (I know how  
the things work, but would like to have a "require 'classes'" kind of  
approach, One Size Classes for those just wanting to get basic  
inheritance etc. without paying much attention to how it's done. Am I  
alone? :)

-asko

David Given kirjoitti 21.4.2006 kello 13.13:

>
> One day I'm going to make a page in the Wiki collecting all the  
> various
> OO systems people have come up with and comparing them...

Reply | Threaded
Open this post in threaded view
|

Re: inheritence and base element syntax cookie ?

Adrian Perez-2
On Sun, 23 Apr 2006 12:28:32 +0300
Asko Kauppi <[hidden email]> wrote:

> I am still puzzled by this area, after 4 years with Lua (I know how  
> the things work, but would like to have a "require 'classes'" kind
> of approach, One Size Classes for those just wanting to get basic  
> inheritance etc. without paying much attention to how it's done. Am
> I alone? :)

I totally agree here. From time to time, discussions arise on the Lua
list about one or another class implementation -- and it always looks
like everyone is crafting his/her own class system (I even made more
than one implementation, just for testing!). I strongly believe that
having a "standard" class system would greatly improve reuse and
interoperability of Lua code. I think it also would/should encourage
a consistent way of making object-oriented bindings of non-Lua code.

But the task of designing such a thing is not effortless if we take
into account one of the principles of Lua: give mechanisms, not policy.
Like the new module system introduced with Lua 5.1 (which I find great)
the "standard" implementation for classes would need to impose some
policy -- but the gains can be more than the (possible) loss of coding
freedom.

Just my 2 cents ;-)

--
Adrian Perez
"Experience is what you gen when you don't get what you want"
                                           -- (Dan Stanford)



--
Adrian Perez
"Experience is what you gen when you don't get what you want"
                                           -- (Dan Stanford)


signature.asc (205 bytes) Download Attachment