26loA

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
LuaWeaver
Party member
Posts: 183
Joined: Wed Mar 02, 2011 11:15 pm
Location: Ohio, USA

26loA

Post by LuaWeaver »

I'm not sure why, but I was clicking random page on the wiki and stumbled across 30log/32 lines of goodness, and decided I'd try to make a better, shorter one.
Well, I did. This is 26loA, or 26 lines of AWESOME (I don't want to beat a dead horse with the "goodness" thing).

Code: Select all

local classes={}

local function new(tab,...)
	local obj=setmetatable({},tab.mt or {})
	getmetatable(obj).__index=tab
	if obj.init then
		obj:init(...)
	end
	return obj
end

function class(name)
	local function add(tab)
		classes[name]=setmetatable({},{__call=function(self,...) return new(tab,...) end,__index=tab})
		_G[name]=classes[name]
	end
	return function(arg)
		if type(arg)=="table" then
			add(arg)
		else
			return function(tab)
				add(setmetatable(tab,{__index=classes[arg]}))
			end
		end
	end
end
And here's a bit of test classes (quite literally, they're "test(?2|3)").

Code: Select all

require 'OO' --assume it is OO.lua in top level

function love.load()
	class "test" {
		asdf=true;
		init=function(self) self.asdf=false end;
		mt={__newindex=function(self,i,v) print(i,v) rawset(self,i,v) end}
	}
	local o=test()
	print(o.asdf) --init works!
	o.asdf=true --mt works!

	class "test2" "test" {
		qwer=false;
	}

	local o2=test2()
	print(o2.asdf) --inheritance works from the parent class!
	o2.qwer=true; --mt inheritance works!

	class "test3" "test2" {
		zxcv=false;
		method=function(self) self.zxcv=not self.zxcv end
	}

	local o3=test3()
	print(o3.asdf) --can fetch everything above it!
	o3:method() --methods work, everything is fine!
end
Not quite basic SECS length though, but I don't think any library supporting inheritance will beat that. :|
"your actions cause me to infer your ego is the size of three houses" -finley
User avatar
bramblez
Citizen
Posts: 50
Joined: Sun Jul 29, 2012 1:21 pm
Location: Russia, Moscow

Re: 26loA

Post by bramblez »

for those who are still learning that whole thing might be very confusing, do you care to explain what is this supposed to be?
User avatar
qaisjp
Party member
Posts: 490
Joined: Tue Sep 04, 2012 10:49 am
Location: United Kingdom
Contact:

Re: 26loA

Post by qaisjp »

1loo. 1 line of obfuscation.
That's all I have to bring to this topic.
Lua is not an acronym.
LuaWeaver
Party member
Posts: 183
Joined: Wed Mar 02, 2011 11:15 pm
Location: Ohio, USA

Re: 26loA

Post by LuaWeaver »

qaisjp wrote:1loo. 1 line of obfuscation.
That's all I have to bring to this topic.
I was waiting for someone to say that.
No thanks.
"your actions cause me to infer your ego is the size of three houses" -finley
User avatar
qaisjp
Party member
Posts: 490
Joined: Tue Sep 04, 2012 10:49 am
Location: United Kingdom
Contact:

Re: 26loA

Post by qaisjp »

LuaWeaver wrote: Not quite basic SECS length though, but I don't think any library supporting inheritance will beat that. :|
https://github.com/qaisjp/openframe/blo ... ss.lua#L19
literally 11 lines if you exclude unneeded content, although that comes without init.
Lua is not an acronym.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: 26loA

Post by Roland_Yonaba »

qaisjp wrote: https://github.com/qaisjp/openframe/blo ... ss.lua#L19
literally 11 lines if you exclude unneeded content, although that comes without init.
Link is dead... :huh:
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: 26loA

Post by Robin »

Roland_Yonaba wrote:Link is dead... :huh:
Oh no!
Image
Help us help you: attach a .love.
User avatar
qaisjp
Party member
Posts: 490
Joined: Tue Sep 04, 2012 10:49 am
Location: United Kingdom
Contact:

Re: 26loA

Post by qaisjp »

Roland_Yonaba wrote:
qaisjp wrote: https://github.com/qaisjp/openframe/blo ... ss.lua#L19
literally 11 lines if you exclude unneeded content, although that comes without init.
Link is dead... :huh:
Robin wrote:
Roland_Yonaba wrote:Link is dead... :huh:
Oh no!
Image
sorry today we decided to revive it with a complete new version ("openFrame 3.0: Reimagined. Rebuilt") into an organization.
i pushed the 2.0 into a branch, https://github.com/projectoraculum/open ... ss.lua#L19
Lua is not an acronym.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 80 guests