Module std.prototype.object

Object Prototype.

This module provides a specialization of the prototype.container.prototype with the addition of object methods. In addition to the functionality described here, object prototypes also have all the methods and metamethods of the prototype.container.prototype.

Note that object methods are stored in the __index field of their metatable, and so cannot also use the __index metamethod to lookup references with square brackets. Use a prototype.container.prototype based object if you want to do that.

Prototype Chain

table
 `-> Container
      `-> Object

Objects

prototype Object prototype.

Module Functions

type (x) Type of an object.

Methods

prototype:clone (...) Return a clone of this object and its metatable.
prototype.mapfields (new, src[, map={}]) Return new with references to the fields of src merged in.


Objects

prototype
Object prototype.

Fields:

  • _init table or function object initialisation (optional)
  • _type string object name (default "Object")

Usage:

     local Object = require "std.prototype.object".prototype
     local Process = Object {
       _type = "Process",
       _init = { "status", "out", "err" },
     }
     local process = Process {
       procs[pid].status, procs[pid].out, procs[pid].err, -- auto assigned
       command = pipeline[pid],                           -- manual assignment
     }

Module Functions

type (x)
Type of an object.

It's conventional to organise similar objects according to a string valued _type field, which can then be queried using this function.

Parameters:

  • x an object

Returns:

    string type of x, or nil if x has no _type metatable entry.

Usage:

       local Object = require "std.object".prototype
       assert (object.type (Object) == "Object")
       local Stack = Object {
         _type = "Stack",
         ...
       }
       local stack = Stack {"some stuff"}
       assert (object.type (stack) == getmetatable (stack)._type)

Methods

prototype:clone (...)
Return a clone of this object and its metatable.

This function is useful if you need to override the normal use of the __call metamethod for object cloning, without losing the ability to clone an object.

Parameters:

  • ... arguments to prototype's _init, often a single table

Returns:

    prototype a clone of this object, with shared or merged metatable as appropriate

See also:

Usage:

     local Node = Object { _type = "Node" }
     -- A trivial FSA to recognize powers of 10, either "0" or a "1"
     -- followed by zero or more "0"s can transition to state 'finish'
     local states; states = {
       start  = Node { ["1"] = states[1], ["0"] = states.finish },
       [1]    = Node { ["0"] = states[1], [""] = states.finish },
       finish = Node {},
     }
prototype.mapfields (new, src[, map={}])
Return new with references to the fields of src merged in.

You can change the value of this function in an object, and that new function will be called during cloning instead of the standard prototype.container.mapfields implementation.

Parameters:

  • new table partially instantiated clone container
  • src table clone argument table that triggered cloning
  • map table key renaming specification in the form {old_key=new_key, ...} (default {})

Returns:

    table merged public fields from new and src, with a metatable of private fields (if any), both renamed according to map

See also:

generated by LDoc 1.4.3 Last updated 2016-02-08 00:31:49