lua-users home
lua-l archive

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


>Just curious what clever methods for object
>instantiation people have come up with..
I have some functions to make OOP easy (in my opinion) with Lua. You define
a class using the class() function passing all super classes:

Button = class(Component, MouseListener)

Then you declare a function in Button that will act as a constructor:

function Button:constructor(left, top, right, bottom, text) 
        self.left = left
        self.top = top
        self.right = right
        self.bottom = bottom
        self.text = text
end

Then the 'index' tag method for classes will call your constructor for you:

ok = Button(10, 10, 100, 30, 'Ok')

Note that when the constructor is called the 'self' parameter is actually an
instance of the class 'Button', not 'Button' itself.

>
>I currently use :
>foo = new_ObjectName(params)
>
>I suppose you could make a 'new' global
>and do something like new.Object...
>
>Or possibly have a global tag for each tag
>type you have which would hold 'static methods'
>and do Object.New or Object.Create...
>
>Those are my only ideas, and I'm wondering if
>there are some tricks to the lua syntax that
>might allow something more... 'sugary' to users