lua-users home
lua-l archive

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


I use a method that is pretty much direct from the blue PiL book, 2nd
edition.

package(..., package.seeall)

function _M:method1(arg1)
	-- do something
end

function _M:new(instance)
	local instance = instance or {}
	setmetatable(instance, self)
	self.__index = self

	-- additional instance initialisation goes here

	return instance
end


This is pretty much your option three. The advantage is that by using
the prototype (or class table) as the metatable for class instances, you
don't require the additional table per instance. It also works well with
inheritance, as described in PiL.

- DC