stdlib 41.2.3 Reference

# Class `std.container`

Container prototype.

A container is a std.object with no methods. It's functionality is instead defined by its *meta*methods.

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.

When making your own prototypes, derive from std.container if you want to access the contents of your objects with the `[]` operator, or from std.object if you want to access the functionality of your objects with named object methods.

Prototype Chain ---------------

table `-> Object `-> Container

## Objects

std.container.Container | Container prototype.

## Objects

std.container.Container
  Container prototype.

  Container also inherits all the fields and methods from std.object.Object.

  ### Fields:

  - _type string object name (default "Container")

  ### See also:

  - std.object
  - std.object.__call

  ### Usage:

      local std = require "std"
      local Container = std.container {}

      local Graph = Container {
        _type = "Graph",
        _functions = {
          nodes = function (graph)
            local n = 0
            for _ in std.pairs (graph) do n = n + 1 end
            return n
          end,
        },
      }
      local g = Graph { "node1", "node2" }
      --> 2
      print (Graph.nodes (g))

generated by LDoc 1.5.0 Last updated 2026-07-25 13:31:42