class
A library for creating classes, implementing inheritance, and working with the object model in Lua.
A class consists of two tables:
- base (
class.base) - the metatable of every object, holds methods and metamethods; - class (
class.create) - the callable table that creates objects, holds static fields.
Functions
class.base | Creates a new class base ( metatable ). |
class.create | Creates a new class from the given base. |
class.init | This function is optional and can be used to re-initialize the object. |
class.isInherited | Checks if the value is an instance of the given class or any of its children. |
class.new | Creates a new class object without calling init. |
#class.base
function class.base(name: string, private?: boolean, parent?: ( Class | unknown )?): Object
Creates a new class base ( metatable ).
If parent is given, methods are looked up through the parent base, and metamethods ( __* keys ) of the parent base are copied into the new base.
Notes:
- metamethods are copied once, at the moment of this call, so the parent base must be fully defined before its children are created;
__indexis never copied, a parent with a custom__indexfunction requires the child to define its own__index;- private objects are
userdata, so they cannot store fields and their data must be kept outside ( e.g. in a weak table ).
| Name | Type | Description |
|---|---|---|
name | string | The name of the class. |
privateoptional | boolean | If true, objects will be userdata instead of table. |
parentoptional | ( Class | unknown )? | The parent class. |
| Type | Name | Description |
|---|---|---|
Object | base | The base of the class. |
#class.create
function class.create(base: Object): Class | unknown
Creates a new class from the given base.
Static fields of the parent class ( including __new, except __inherited ) are copied into the new class, then __inherited of every ancestor class is called, from the direct parent up to the root.
| Name | Type | Description |
|---|---|---|
base | Object | The base object, aka metatable. |
| Type | Name | Description |
|---|---|---|
Class | unknown | cls | The class. |
#class.init
function class.init(base: Object, obj: Object, ...?: any): Object
This function is optional and can be used to re-initialize the object.
Calls the base initialization function, <b>if it exists</b>, and returns the given object.
| Name | Type | Description |
|---|---|---|
base | Object | The base object, aka metatable. |
obj | Object | The object to initialize. |
...optional | any | Arguments to pass to the constructor. |
| Type | Name | Description |
|---|---|---|
Object | object | The initialized object. |
#class.isInherited
function class.isInherited(obj: any, cls: Class | Object): boolean
Checks if the value is an instance of the given class or any of its children.
Plain metatables ( not created by class.base ) only match directly, without inheritance.
| Name | Type | Description |
|---|---|---|
obj | any | The object to check for being an instance of the given class. |
cls | Class | Object | The class or its base to check against. |
| Type | Name | Description |
|---|---|---|
boolean | is_instance | true if obj is an instance of the given class, false otherwise. |
#class.new
function class.new(base: T): T
Creates a new class object without calling __init.
| Name | Type | Description |
|---|---|---|
base | T | The base object, aka metatable. |
| Type | Name | Description |
|---|---|---|
T | obj | The new object. |