Module:Repr/doc: Difference between revisions
Add category |
expand the repr function docs |
||
Line 1: | Line 1: | ||
This module contains functions for generating string representations of Lua objects. It is inspired by Python's [https://docs.python.org/3/library/functions.html#repr Python's repr] function. |
|||
== Usage == |
== Usage == |
||
First, import the module. |
|||
Call the function with any single value and it will return a string. |
|||
<syntaxhighlight lang="lua"> |
<syntaxhighlight lang="lua"> |
||
local |
local mRepr = require("Module:Repr") |
||
</syntaxhighlight> |
|||
Then you can use the functions it contains. The documentation for each function is below. |
|||
local myTable = { |
|||
hello = "world"; |
|||
== Functions == |
|||
score = 5; |
|||
isCool = true; |
|||
=== repr === |
|||
} |
|||
print(repr(myTable)) --> {hello = "world", isCool = true, score = 5} |
|||
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, <syntaxhighlight lang="lua" inline>mRepr.repr({bool = true, number = 6, str = "hello world"})</syntaxhighlight> will output the string <code>{bool = true, number = 6, str = "hello world"}</code>. |
|||
Basic syntax: |
|||
<syntaxhighlight lang="lua"> |
|||
mRepr.repr(value) |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||
Full syntax: |
|||
The second value can be a table of options, which allows you to **pretty-print** with newlines: |
|||
<syntaxhighlight lang="lua"> |
<syntaxhighlight lang="lua"> |
||
mRepr.repr(value, options) |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||
Parameters: |
|||
* <code>value</code>: The value to convert to a string. This can be any Lua value. |
|||
* <code>options</code>: A table of options. This parameter is optional. |
|||
The following options can be specified in the options table: |
|||
* <code>pretty</code>: If true, output the string in "pretty" format (as in [[prettyprint|pretty-printing]]). This will add new lines and indentation between table items. If false, format everything on one line. The default is false. |
|||
* <code>tabs</code>: If true, indent with tabs; otherwise, indent with spaces. The default is true. This only has an effect if <code>pretty</code> is true. |
|||
* <code>spaces</code>: The number of spaces to indent with, if <code>tabs</code> is false. The default is 4. This only has an effect if <code>pretty</code> is true. |
|||
* <code>semicolons</code>: If true, table items are separated with semicolons. If false, they are separated with spaces. The default is false. |
|||
* <code>sortKeys</code>: 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 [[mw:LUAREF#pairs|pairs]] function). The default is true. |
|||
* <code>depth</code>: The indentation depth to output the top-level object at. The default is 0. This only has an effect if <code>pretty</code> is true. |
|||
Features: |
|||
* The function handles cyclic tables gracefully; when it detects a cycle, the inner table is rendered as <code>{CYCLIC}</code>. |
|||
* <code>__tostring</code> metamethods are automatically called if they are available. |
|||
Here is an example that shows off all the bells and whistles: |
Here is an example that shows off all the bells and whistles: |
Revision as of 05:46, 27 February 2021
This module contains functions for generating string representations of Lua objects. It is inspired by Python's Python's repr function.
Usage
First, import the module.
local mRepr = require("Module:Repr")
Then you can use the functions it contains. The documentation for each function is below.
Functions
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, mRepr.repr({bool = true, number = 6, str = "hello world"})
will output the string {bool = true, number = 6, str = "hello world"}
.
Basic syntax:
mRepr.repr(value)
Full syntax:
mRepr.repr(value, options)
Parameters:
value
: The value to convert to a string. This can be any Lua value.options
: A table of options. This parameter is optional.
The following options can be specified in the options table:
pretty
: 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.tabs
: If true, indent with tabs; otherwise, indent with spaces. The default is true. This only has an effect ifpretty
is true.spaces
: The number of spaces to indent with, iftabs
is false. The default is 4. This only has an effect ifpretty
is true.semicolons
: If true, table items are separated with semicolons. If false, they are separated with spaces. The default is false.sortKeys
: 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.depth
: The indentation depth to output the top-level object at. The default is 0. This only has an effect ifpretty
is true.
Features:
- The function handles cyclic tables gracefully; when it detects a cycle, the inner table is rendered as
{CYCLIC}
. __tostring
metamethods are automatically called if they are available.
Here is an example that shows off all the bells and whistles:
local repr = require("Module:Repr").repr
local myTable = {
hello = "repr",
usefulness = 9001,
isEasyToUse = true,
array = {"numerical", "arrays", "are", "easy"},
land = workspace["a b c"]["1 2 3"],
subTables = {
moreInfo = "calls itself recursively to print sub-tables"
},
usesToString = {__tostring = function () return "__tostring functions are called automatically" end},
["$YMBOL$"] = "keys that aren't Lua identifiers are quoted";
[{also = "tables as keys work too";}] = "in case you need that",
cyclic = {note = "cyclical tables are printed as just {CYCLIC}"}
}
-- create a cycle:
myTable.cyclic.cyclic = myTable.cyclic
local reprSettings = {
pretty = false; -- print with \n and indentation?
semicolons = false; -- when printing tables, use semicolons (;) instead of commas (,)?
sortKeys = true; -- when printing dictionary tables, sort keys alphabetically?
spaces = 3; -- when pretty printing, use how many spaces to indent?
tabs = false; -- when pretty printing, use tabs instead of spaces?
}
print(repr(myTable, reprSettings))