Module:Repr/doc

This module contains functions for generating string representations of Lua objects. It is inspired by Python's repr function.

Usage
To use the module, first you have to import it.

Then you can use the functions it contains. The documentation for each function is below.

repr
This function generates a string representation of any given Lua object. The idea is that if you copy the string this function produces it, and paste it back into a Lua program, then you should be able to reproduce the original object. This doesn't work for all values, but it should hold for simple cases.

For example,  will output the string.

Basic syntax:

Full syntax:

Parameters:
 * : The value to convert to a string. This can be any Lua value. This parameter is optional, and defaults to.
 * : A table of options. This parameter is optional.

The following options can be specified in the options table:
 * : If true, output the string in "pretty" format (as in pretty-printing). This will add new lines and indentation between table items. If false, format everything on one line. The default is false.
 * : If true, indent with tabs; otherwise, indent with spaces. The default is true. This only has an effect if  is true.
 * : The number of spaces to indent with, if  is false. The default is 4. This only has an effect if   is true.
 * : If true, table items are separated with semicolons. If false, they are separated with spaces. The default is false.
 * : If true, sort table keys in lexical order, after other table key formatting has been applied (such as adding square brackets). If false, table keys are output in arbitrary order (the order they are processed by the pairs function). The default is true.
 * : The indentation depth to output the top-level object at. The default is 0. This only has an effect if  is true.

Features:
 * The function handles cyclic tables gracefully; when it detects a cycle, the inner table is rendered as.
 * metamethods are automatically called if they are available.
 * The sequence part of a table is always rendered as a sequence. If there are also key-value pairs, they will be rendered after the sequence part.

Here is an example that shows off all the bells and whistles:

This logs the following:

{  ["$YMBOL$"] = "Keys that aren't Lua identifiers are quoted", [{     also = "Tables as keys work too" }] = "in case you need that", cyclic = { cyclic = {CYCLIC}, note = "cyclical tables are printed as just {CYCLIC}" },  hello = "repr", isEasyToUse = true, mixed = { "a", "sequence", with = "key-value pairs" },  sequence = { "a", "sequence", "table" },  subTables = { moreInfo = "Calls itself recursively on sub-tables" },  usefulness = 100, usesToString = __tostring functions are called automatically }

invocationRepr
This function generates a string representation of a function invocation.

Basic syntax:

Full syntax:

Parameters:
 * : The function name. This parameter is required, and must be a string.
 * : The function arguments. This should be sequence table. The sequence items can be any Lua value, and will each be rendered using the function. This argument is optional.
 * : A table of options. The options are the same as for the repr function. This argument is optional.

Examples:

Result: