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}
("a is an '%s' with values: %t", a:name(), a)
putln("c is a '%s' with values: %t", c:name(), c) putln
- 1
-
Create
CheckedArray
as 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:
("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)) putln
Output
a is an Array: true
c is an Array: true a is a CheckedArray: false
Example: Concatenation
local a = Array{1,2,3}
local b = Array{3,4,5}
local c = a .. b
("a: %t", a)
putln("b: %t", b)
putln("c: %t", c) putln
Output: Concatenation
- 1
-
a
is unchanged. - 2
-
b
is unchanged. - 3
-
c
has the same elements asa:append(b)
so there is duplication in this instance.