Ideas for sugary 'new object' syntax ?

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

Ideas for sugary 'new object' syntax ?

kaishaku13
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


Reply | Threaded
Open this post in threaded view
|

Re: Ideas for sugary 'new object' syntax ?

Denis Andreev
k> I currently use :
k> foo = new_ObjectName(params)

For all objects we use:
object = Object { <params> }

--Denq


Reply | Threaded
Open this post in threaded view
|

Re: Ideas for sugary 'new object' syntax ?

Thomas Wrensch-2
In reply to this post by kaishaku13
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
> 
> 


Reply | Threaded
Open this post in threaded view
|

RE: Ideas for sugary 'new object' syntax ?

Leiradella
In reply to this post by kaishaku13
>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

Reply | Threaded
Open this post in threaded view
|

Re: Ideas for sugary 'new object' syntax ?

kaishaku13
In reply to this post by Denis Andreev
I forgot to mention this is for userdata on the C/C++ side,
not for a pure Lua implemented class/object setup.

--- In lua-l@y..., Denis Andreev <denq@m...> wrote:
> 
> k> I currently use :
> k> foo = new_ObjectName(params)
> 
> For all objects we use:
> object = Object { <params> }
> 
> --Denq


Reply | Threaded
Open this post in threaded view
|

Re: Ideas for sugary 'new object' syntax ?

Brian Chin
Actually, the syntax here doesn't require that Object is a native lua type. It's equivalent to metatable(Object).call({...}), with which you can make anything you want (userdata, or whatever).
- Brian

kaishaku13 wrote:

I forgot to mention this is for userdata on the C/C++ side,
not for a pure Lua implemented class/object setup.

--- In lua-l@y..., Denis Andreev <denq@m...> wrote:

k> I currently use :
k> foo = new_ObjectName(params)

For all objects we use:
object = Object { <params> }

--Denq