lua-users home
lua-l archive

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


I put together a C lib that adds some meta tables and functions for making
objects in 4.1-work4. It defines a class stored in the global variable
Object (I'm a big Smalltalk fan). This is what I do for syntax:

    -- Declare Shape, Circle, and Square classes
    Shape = class(Object)
    Circle = class(Shape)
    Square = class(Shape)

Not much fun, but very flexable. I origionally used this:

    -- Declare a Square class
    Shape = class {
        _parent = Object,
        _init = function(self, x, y, side)
                self.x = x
                self.y = y
                self.side = side
            end
        -- put rest of definition here
    }

In both cases new objects were created this way:

    mySquare = Square:new(10,10,20)

The new function (inherited from Object) calls _init if it exists.

One interesting variation that I consdered for defining a class was this:

    Square = class(Shape) {
        _init = function(self,x,y,side)
                self.x = x
                self.y = y
                self.side = side
            end
        -- rest of definition goes here
    }

This was done by having class(Shape) return a function that built a new
object with Shape as the superclass. I decided it was too cute and gave up
on it. Besides it looks WAY to much like C++!

  - Tom

On Wed, 13 Mar 2002, kaishaku13 wrote:

> Just curious what clever methods for object
> instantiation people have come up with..
> 
> 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
> 
>