I know, Luva and JaC are cool and probably better, but this is pure lua, so it will work both inside and outside CC and has properties and member hiding.
Features:
-Classes with short declaration
-Classes are instances themself
-Multi inheritance
-Member hiding (members beginning with '_')
-Properties (_get_<name> for declaring the getter, _set_<name> for declaring setter, see the part Properties for info how to use them)
-Intelligent constructing (different ways for building clever inheritance trees)
-Util functions that I usually need and I copy-paste them lots of times (string.split, table.contains, table.deepContains, table.merge)
-Fastclasses = a different, non-metatable, better performance class implementation
How to use it = Tutorial
Classes
Spoiler
Simple usage
--declare class:
a = class {
i = 2; --public fields begin with letter
_x = 4; --private fields begin with underscore
show = function(this) --functions can be private and public, too
print(this.i)
print(this.o)
print(this._x) -- here you can access private vars because it's inside the class
end;
}
--you can actually use the class itself, because the class is an instance itself
a.o = 4 -- you can add extra fields later, but only public
a._x = 5 --this will actually not change anything, my small library will ignore it
a.show() -- will print 2 2 4
-- create a clone (instance)
b = a()
b.show() -- will show 2 2 4
b.i = 7 -- will set b's i to 7, but not a's because b is (almost) independent now
print(b.i) -- output is 7
b.show() -- output is 7 2 4
a.show() -- output is 2 2 4
Method overriding:
a = class {
_privatefunc = function(this)
print("hi there from privatefunc from a")
end;
show = function(this)
this._privatefunc()
print("hi from show from a")
end;
}
b = a { --extend a
show = function(this)
print("hi there from show from b")
end;
}
b.show() --output: hi there from show from b
c = class( --different way of extending
{
_privatefunc = function(this)
print("hi there from privatefunc from c")
end;
}, a)
c.show() --output: hi there from privatefunc from c hi from show from a
--as you can see everything is virtual by default, like in Java
--reminder:
c._privatefunc() -- of course this is attempt to call nil outside the class declaration
Properties:
a = class {
_i = 2;
show = function(this)
print(this.i)
end;
_get_i = function(this)
print("i was requested")
return this._i
end;
_set_i = function(this,value)
print("i will be "..tostring(value))
this._i = value
end;
}
a.i = 3 --output: i will be 3
a.show() --output i was requested 3
Extra knowledge that you will need (maybe):- You can replace the getter with any non-function value, too, that results in a read-only field (from outside)
- You need to call functions with '.' - NOW EVERYWHERE - and declare them with extra "this" parameter (must be first)
- Every returned class (where you can't use private fields) has a field called "class", where you can use private methods and a field called "super", which contains all the superclasses (warning: if you create a class like this: ac {…} (ac = a parent class) the number 1 parent class is everything in the brackets, the number 2 is the class 'ac')
Fastclasses
Spoiler
Example says everything:f = fastclass {
private = {
l = function(this)
return this.public.i()
end;
i = 7;
};
public = {
l = function(this)
print(this.private.l())
end;
i = function(this)
return this.private.i + this.public.a
end;
a = 5;
};
}
f.l() -- output 12
Note: inheritance doesn't work with fastclasses yet :(/>/>
You can use table.merge for every private and every public and THEN use them for making a fastclass
Spoiler
I am not sure if anybody will be intrested enough in this that he'll remember version names so I'll not use them2016 march:
26th
Added fastclasses
Now in classes you have to call methods with '.'
You can use properties inside classes with the same syntax as outside
25th
Original release
//Util function documentation: TODO
Download: http://pastebin.com/KVW9bYh5
pastebin get KVW9bYh5class
Edit: sorry for short var names I wrote this on android and tested it in QLua