lua-users home
lua-l archive

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


On Tue, Mar 19, 2013 at 6:02 PM, Rena <hyperhacker@gmail.com> wrote:
On Tue, Mar 19, 2013 at 2:56 PM, phil anc <philanc@gmail.com> wrote:
> ...And I don't want to wrap my head every time around metatables and __index
> :-)   so I came up with the following 'class' definition:
>

Metatables aren't terribly difficult, really. Here's how I usually
define objects:
 
Of course, you are right, this is not so difficult. This is why I put a smiley in my post!  My point is rather that when I manipulate a lot of small, lightweight classes,  I don't want to repeat the kind of thing below each time I create a new class:

    return setmetatable(self, {__index=myObjectMethod})

The class() object I described just encapsulates this stuff and allows to write:

    -- create a new class
    myObject = class()

    -- define methods  (no need for a myObjectMethod table here)
    function myObject:print_xyz() . . . end

    -- then, to create an instance
    foo = myObject{x=1, y=2, z=3}

So, just a little less boilerplate to repeat for each class.

Phil