Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
Centauri Soldier
Posts: 42
Joined: Mon May 21, 2012 6:38 am


Post by Centauri Soldier »

I decided to modify the wondeful 32 Lines of Goodness to suit my own project and it turned out to be pretty useful for more than that one project. So, I thought I'd share it here for anyone else who might need such functionality. I know, it's not 32 lines anymore. I left that in the name to honor the original author.

32LoGEx now allows for all metamethods for your custom class.
32LoGEx now creates a pseudo-type for your custom class which can be returned by the lua type() function.

Code: Select all

This is a modification of the '32 Lines of Goodness' found at https://love2d.org/wiki/32_lines_of_goodness.

Added Features:
	Allows the creation of all known metamethods for each class.
		class "MyClass" {};
		function MyClass:__tostring() return "This is what I want to return." end
		local obj = MyClass:new();		
		-> Output would be "This is what I want to return."
	Creates pseudo-types of each class created by overriding the lua type() function.
		class "CoolClass" {};
		local obj = CoolClass:new();
		-> output would be "CoolClass"

Copyright © 2019 AMSPublic.org

This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

    1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
    2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
    3. This notice may not be removed or altered from any source distribution.

local mt_class = {}
function mt_class:extends(parent)
	self.super = parent
	setmetatable(mt_class, {__index = parent})
	parent.__members__ = parent.__members__ or {}
	return self
local function define(class, members)
	class.__members__ = class.__members__ or {}
	for k, v in pairs(members) do
		class.__members__[k] = v
	function class:new(...)
		local newvalue = {}
		for k, v in pairs(class.__members__) do
			newvalue[k] = v
							__index = 		class,
							__add = 		class.__add,
							__concat =		class.__concat,
							__div =			class.__div,
							__eq =			class.__eq,
							__le =			class.__le,
							__len =			class.__len,
							__lt =			class.__lt,
							__mod =			class.__mod,
							__mul =			class.__mul,
							__newindex = 	class.__newindex,
							__pow =			class.__pow,
							__sub =			class.__sub,
							__tostring = 	class.__tostring,
							__type =		class.__type,
							__unm =			class.__unm,
		if newvalue.__init then
		return newvalue;

function class(name)
	local newclass = {
		__add = 		function() end,
		__concat = 		function() end,
		__div = 		function() end,
		__eq = 			function() end,
		__le = 			function() end,
		__len = 		function() end,		
		__lt = 			function() end,
		__mod = 		function() end,
		__mul = 		function() end,
		__newindex = 	function() end,
		__pow = 		function() end,
		__sub = 		function() end,
		__tostring = 	function() end,
		__type =		name,
		__unm = 		function() end,		
	_G[name] = newclass;
	return setmetatable(newclass, {__index = mt_class, __call = define});

local _type = type;
function type(vObj)
	local sType = _type(vObj);
	if (sType == "table") then
		local tMeta = getmetatable(vObj);
		if (tMeta) then
			if (tMeta["__type"]) then
				sType = tMeta["__type"];

	return sType;
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests