[ create a new paste ] login | about

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

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

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

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

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

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

	for _, base in ipairs({...})  do
		for k, v in next, base do
			rawset(class, k, v)
		end
	end
	
	-- We want to override from base classes
	rawset(class, "__name", name)
	rawset(class, "__index", class)

	return class
end


Output:
No errors or program output.


Create a new paste based on this one


Comments: