dreamworkAPI
Ctrl K
GitHub
libraryDocs / class

class

librarysharedmenusource

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.baseCreates a new class base ( metatable ).
class.createCreates a new class from the given base.
class.initThis function is optional and can be used to re-initialize the object.
class.isInheritedChecks if the value is an instance of the given class or any of its children.
class.newCreates a new class object without calling init.

#class.base

sharedmenunodiscardsource
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;
  • __index is never copied, a parent with a custom __index function 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 ).
Parameters
NameTypeDescription
namestringThe name of the class.
privateoptionalbooleanIf true, objects will be userdata instead of table.
parentoptional( Class | unknown )?The parent class.
Returns
TypeNameDescription
ObjectbaseThe base of the class.

#class.create

sharedmenusource
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.

Parameters
NameTypeDescription
baseObjectThe base object, aka metatable.
Returns
TypeNameDescription
Class | unknownclsThe class.

#class.init

sharedmenunodiscardsource
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.

Parameters
NameTypeDescription
baseObjectThe base object, aka metatable.
objObjectThe object to initialize.
...optionalanyArguments to pass to the constructor.
Returns
TypeNameDescription
ObjectobjectThe initialized object.

#class.isInherited

sharedmenunodiscardsource
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.

Parameters
NameTypeDescription
objanyThe object to check for being an instance of the given class.
clsClass | ObjectThe class or its base to check against.
Returns
TypeNameDescription
booleanis_instancetrue if obj is an instance of the given class, false otherwise.

#class.new

sharedmenunodiscardsource
function class.new(base: T): T

Creates a new class object without calling __init.

Parameters
NameTypeDescription
baseTThe base object, aka metatable.
Returns
TypeNameDescription
TobjThe new object.
Generated from the dreamwork meta branch.