Page 1 of 1

26loA

Posted: Fri Jun 07, 2013 3:53 am
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. :|

Re: 26loA

Posted: Fri Jun 07, 2013 8:24 am
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?

Re: 26loA

Posted: Fri Jun 07, 2013 7:16 pm
by qaisjp
1loo. 1 line of obfuscation.
That's all I have to bring to this topic.

Re: 26loA

Posted: Fri Jun 07, 2013 10:54 pm
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.

Re: 26loA

Posted: Fri Jun 07, 2013 11:22 pm
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.

Re: 26loA

Posted: Sat Jun 08, 2013 7:58 pm
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:

Re: 26loA

Posted: Sat Jun 08, 2013 8:29 pm
by Robin
Roland_Yonaba wrote:Link is dead... :huh:
Oh no!
Image

Re: 26loA

Posted: Sat Jun 08, 2013 9:34 pm
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