Another Lua OOP Implementation.
aloopi provides a neat and clean solution to OOP in Lua that includes:
- Classes
- Inheritance
- Static members
- Public and private access
- Custom meta
- With the exception of
__index, __newindex
- With the exception of
- Static typing for variables and functions
- Supports multidimensional arrays
f([,]num)-> two-dimensional array of infinite numbers
- Supports custom class types
f(Apple)-> Apple class
- Supports dictionaries
table<str, num>-> dictionary with string keys and number values
- Supports optionals
a: num?---> a variable that can benilor anumber.
- Supports multidimensional arrays
Member access
lowercase for instance members.
- Instance members can access instance members (and static by referencing the actual class not self).
Uppercase for static members.
- Static members can only access other static members.
const before variable names to define a constant.
- example:
const FAVORITE_NUM
_ prefix for private members.
__ prefix for meta members.
Defining a class
-- aloopi.class (name of class) (super class) (class members)
local Fruit = aloopi.class 'Fruit' () {
-- For objects to be instantiated, a class must define `__init()` as well.
['__init(str, num)'] = function(self, name, yumminess)
end;
}
-- Instantiation by calling the class.
local apple = Fruit('Apple', 10)This module returns:
BaseClass class(string name) -> (BaseClass super_class) -> (table members)string typeof(any object)- retrieves
__typefrom given class ortype(object)/typeof(object)
bool instanceof(BaseClass object, string class_name)- returns
trueif object is derived fromclass_name.
['const FAVORITE_NUM'] = 5- constants cannot be statically typed because they cannot change to begin with.
- you can define functions as const
const f(num)
['name: str'] = ''nameis the name of the variable.:operator is used to denote type.stris shorthand for the datatypestring.
['f(str, num)'] = function(name, age)fis the name of the function.- parenthesis are required to identify function.
stris shorthand forstring.numis shorthand fornumber.
['f(str, any, num)'] = function(name, something, age)`- you can omit
anyas in the shorthand version:f(str,, num).
str = stringnum = numberfn = functiontab = tablebool = booleanusd = userdatathr = threadany = any- Not a datatype but who cares?
Arrays have three main qualities:
- Datatype
- Size*
- Number of dimensions*
The preferred method of defining an array is of the form:
'[' [size ','...] ']' datatypeyou can define all three of these qualities.
f([]str)---> array of infinite stringf([5]num)---> array of five numbersf([,]num)---> two-dimensional array of infinite numbers
The other way of defining an array is of the form:
'table<' datatype '>'This defines a one dimensional array of infinite datatype.
Dictionaries are defined as:
'table<' key_datatype ',' value_datatype '>'Example:
dict: tab<str, num>---> keys:string, values:numbers
Define variable arguments in function declarations with:
datatype '...'Examples:
f(str...)---> variable arguments of type stringf(num, []bool...)---> number, infinite arrays of infinite bools
Define a variable or parameter to be optional with ?.
['random(num, num?)'] = function(arg1, maybe_a_number_maybe_nil)
['favorite_word: str?'] = nilstatement = const_declaration | key_declaration
const_declaration = 'const' key_declaration
key_declaration = var_declaration | fn_declaration
var_declaration = IDENTIFIER ':' type_expression
fn_declaration = IDENTIFIER '(' fn_type_expression... ')'
fn_type_expression = type_expression ['...']
type_expression = array_expression | table_expression | datatype
array_expression = '[' [NUMBER ',']... ']' datatype
table_expression = 'table<' type_expression [',' type_expression] '>'
datatype = identifier ['?']