lua-users home
lua-l archive

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


I'm defining an "object class" by using a table, Which is better "style" ...

Should I define the "member functions" inside the table declarations for
the class, like this:

    FooClassTag = newtag();
    FooClassInstanceTag = newtag();
    FooClass = {
	instanceTag = FooClassInstanceTag,
	dataMember = defaultValue,
	-- etc.

	-- "member functions"
	memberFunc = function (self, param)
	    -- stuff ...
	end
    }

Or should I declare them outside of the declaration of the "Class" ...

    FooClassTag = newtag();
    FooClassInstanceTag = newtag();
    FooClass = {
	instanceTag = FooClassInstanceTag,
	dataMember = defaultValue,
	-- etc.
    }
    settag(FooClass,FooClassTag);

    function FooClass:memberFunc ( param )
	-- stuff
    end

One advantage to the second method is that I don't have to declare
the implicit 'self' parameter, because of the the 'function table:name' 
syntactical sugar ... I also like it because it makes the data

I'm abusing the Lua object model a bit, I'll instantiate an "instance" of 
the class with a "new" function which copies the clas table and sets it's 
tag to the 'instangce tag'.  I'll also make the "Class" tables readonly 
with tag methods.  (I will likely allow a 'constructor' function to be 
defined in the class table, which the 'new' function will call).

The whole purpose of this is to add some 'type safety' to the code that 
I'm exposing to "users". (The "C" runtime will first execute a compiled
version of all of the exposed "classes" before running the user code).

Am I barking up the wrong tree here?
--
Mike Cuddy (mcuddy@FensEnde.com, MC312), Programmer, Daddy, Human.
Fen's Ende Software, Redwood City, CA, USA, Earth, Sol System, Milky Way.
I remember asking why ... Let it rain, and protect us from this Cruel Sun.

       Join CAUCE: The Coalition Against Unsolicited Commercial E-mail.  
                          <http://www.cauce.org/>