Log In  


I decided to make a simple class implementation. Here's what I managed to come up with:

function class(c)
	name=c[1]
	_𝘦𝘯𝘷[name]=function(...)
		local initargs={...}
		local obj={___fnccore={}}
		for k,f in pairs(c)do
			if type(f)=='function'then
				obj.___fnccore[k]=f
				obj[k]=function(...)
					obj.___fnccore[k](obj,...)
				end
			end
		end
		obj.__init__(unpack(initargs))
		return obj
	end
end

The (preferred) syntax to use this with looks like this:

class { 'classname'
	__init__=function(self,args)
		-- Stuff goes here --
	end,
	-- Other functions here with "self" or something like that as the first argument --
}

Here's an example:

class { 'printer',
	__init__=function(self,a,b)
		self.a=a
		self.b=b
	end,
	thing=function(self,c)
		print(self.a..self.b..c)
	end
}
test=printer('one ','two')
test.thing(' three')

The output for the above would be:

one two three

Here is the cartridge containing the above example:

Cart #graffiticlasses-0 | 2021-06-01 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA


Feedback would be nice!



have you looked at metatable?
see: https://www.lua.org/pil/16.html


I know what metatables are, yes. What's your point?


actually your framework does something native to lua, eg passing self to a function call.
replace:

test.thing(3)

by:

test:thing(3)

and ‘self’ will automatically be the first parameter


The reason I picked my style of 'self', is because my main programming language is Python and I wanted it to feel like that.



[Please log in to post a comment]