Module:Repr/doc
Appearance
![]() | This is a documentation subpage for Module:Repr. It may contain usage information, categories and other content that is not part of the original module page. |
repr
. is a simple Lua function that works a lot like Python's repr. It converts a Lua value into a printable string.
Usage
Call the function with any single value and it will return a string.
local repr = require("Module:Repr").repr
local myTable = {
hello = "world";
score = 5;
isCool = true;
}
print(repr(myTable)) --> {hello = "world", isCool = true, score = 5}
The second value can be a table of options, which allows you to **pretty-print** with newlines:
print(repr(myTable, {pretty=true}))
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))