Class std.container
Container object.
A container is a std.object with no methods. It's functionality is instead defined by its metamethods.
Where an Object uses the __index
metatable entry to hold object
methods, a Container stores its contents using __index
, preventing
it from having methods in there too.
Although there are no actual methods, Containers are free to use
metamethods (__index
, __sub
, etc) and, like Objects, can supply
module functions by listing them in _functions
. Also, since a
std.container is a std.object, it can be passed to the
std.object module functions, or anywhere else a std.object is
expected.
Container derived objects returned directly from a require statement may also provide module functions, which can be called only from the initial prototype object returned by require , but are not passed on to derived objects during cloning:
> Container = require "std.container"
> x = Container {}
> = Container.prototype (x)
Object
> = x.prototype (o)
stdin:1: attempt to call field 'prototype' (a nil value)
...
To add functions like this to your own prototype objects, pass a table
of the module functions in the _functions
private field before
cloning, and those functions will not be inherited by clones.
> Container = require "std.container"
> Graph = Container {
>> _type = "Graph",
>> _functions = {
>> nodes = function (graph)
>> local n = 0
>> for _ in pairs (graph) do n = n + 1 end
>> return n
>> end,
>> },
>> }
> g = Graph { "node1", "node2" }
> = Graph.nodes (g)
2
> = g.nodes
nil
When making your own prototypes, start from std.container if you
want to access the contents of your objects with the []
operator, or
std.object if you want to access the functionality of your objects
with named object methods.
Tables
std.container | Container prototype. |
Metamethods
std.container:__call (...) | Return a clone of this container. |
std.container:__tostring () | Return a string representation of this container. |
std.container:__totable () | Return a table representation of this container. |
Tables
- std.container
-
Container prototype.
Fields:
- _init table or function a table of field names, or initialisation function, used by __call
- _functions nil or table a table of module functions not copied by std.object.__call
- _type string type of Container, returned by std.object.prototype (default "Container")
Metamethods
- std.container:__call (...)
-
Return a clone of this container.
Parameters:
- ...
arguments for
_init
Returns:
-
std.container
a clone of the called container.
See also:
- ...
arguments for
- std.container:__tostring ()
-
Return a string representation of this container.
Returns:
-
string
stringified container representation
See also:
- std.container:__totable ()
-
Return a table representation of this container.
Returns:
-
table
a shallow copy of non-private container fields
See also: