[ create a new paste ] login | about

Link: http://codepad.org/3Y9SGsPL    [ raw code | fork ]

Lua, pasted on Apr 21:
local class_mt = {
	__name = "class";
}

function class_mt:__call(...)
	local obj = setmetatable({}, self)
	if type(obj.__init) == 'function' then
		obj:__init(...)
	end
	return obj
end

function class_mt:__tostring()
	return 'class ' .. self.__name
end

local function obj__tostring()
	return "instance of " .. self.__name
end

return function(name, ...)
	assert(type(name) == 'string', 'Invalid class name')
	
	local class = setmetatable({
		__name = name;
		__tostring = obj__tostring;
		__index = class;
	}, class_mt)

	for _, base in ipairs({...})  do
		for k, v in next, base do
			rawset(class, k, v)
		end
	end

	return class
end


Output:
No errors or program output.


Create a new paste based on this one


Comments: