Lulu Arrays — Metamethods etc.
Introduction
If you have imported the lulu.Array class as
local Array = require 'lulu.Array'then you have access to the following methods & metamethods.
Array:is_instance(obj)
Returns true if obj is an instance of the Array class or a subclass.
Array:name()
Returns self.__name which is just Array for the base class.
Array.__call
This make any Array(...) call a synonym for Array:new.
Array.__eq
If a and b are Array instances this points code lke a == b and a ~= b use Array:eq under the covers.
Array.__tostring
If a is an Array instance this points code lke print(a) or tostring(a) to {Array.tostring} under the covers.
Array.__name
This is a piece of string data occasionally used by Lua and also returned by the Array:name() method above. It is set to “Array” for the base class.
Array.__concat
If a and b are Array instances then we have overloaded a .. b to return a:copy():append(b).
Example
1local CheckedArray = Array:subclass("CheckedArray")
local a = Array{1,2,3}
local c = CheckedArray{1,2,3}
putln("a is an '%s' with values: %t", a:name(), a)
putln("c is a '%s' with values: %t", c:name(), c)- 1
-
Create
CheckedArrayas a subclass ofArray.
Output
a is an 'Array' with values: [ 1, 2, 3 ]
c is a 'CheckedArray' with values: [ 1, 2, 3 ]Note that any CheckedArray is also an Array but not vice-versa:
putln("a is an Array: %s", Array:is_instance(a))
putln("c is an Array: %s", Array:is_instance(c))
putln("a is a CheckedArray: %s", CheckedArray:is_instance(a))Output
a is an Array: true
c is an Array: true
a is a CheckedArray: falseExample: Concatenation
local a = Array{1,2,3}
local b = Array{3,4,5}
local c = a .. b
putln("a: %t", a)
putln("b: %t", b)
putln("c: %t", c)Output: Concatenation
- 1
-
ais unchanged. - 2
-
bis unchanged. - 3
-
chas the same elements asa:append(b)so there is duplication in this instance.