Module
Import
Section titled “Import”The import statement is a syntax sugar for requiring a module or help extracting items from an imported module. The imported items are const by default.
-- used as table destructuringdo import insert, concat from table -- report error when assigning to insert, concat import C, Ct, Cmt from require "lpeg" -- shortcut for implicit requiring import x, y, z from 'mymodule' -- import with Python style from 'module' import a, b, c
-- shortcut for requring a moduledo import 'module' import 'module_x' import "d-a-s-h-e-s" import "module.part"
-- requring module with aliasing or table destructuringdo import "player" as PlayerModule import "lpeg" as :C, :Ct, :Cmt import "export" as {one, two, Something:{umm:{ch}}}-- used as table destructuringdo import insert, concat from table -- report error when assigning to insert, concat import C, Ct, Cmt from require "lpeg" -- shortcut for implicit requiring import x, y, z from 'mymodule' -- import with Python style from 'module' import a, b, c
-- shortcut for requring a moduledo import 'module' import 'module_x' import "d-a-s-h-e-s" import "module.part"
-- requring module with aliasing or table destructuringdo import "player" as PlayerModule import "lpeg" as :C, :Ct, :Cmt import "export" as {one, two, Something:{umm:{ch}}}Import Global
Section titled “Import Global”You can import specific globals into local variables with import. When importing a chain of global variable accessings, the last field will be assigned to the local variable.
do import tostring import table.concat print concat ["a", tostring 1]do import tostring import table.concat print concat ["a", tostring 1]Automatic Global Variable Import
Section titled “Automatic Global Variable Import”You can place import global at the top of a block to automatically import all names that have not been explicitly declared or assigned in the current scope as globals. These implicit imports are treated as local consts that reference the corresponding globals at the position of the statement.
Names that are explicitly declared as globals in the same scope will not be imported, so you can still assign to them.
do import global print "hello" math.random 3 -- print = nil -- error: imported globals are const
do -- explicit global variable will not be imported import global global FLAG print FLAG FLAG = 123do import global print "hello" math.random 3 -- print = nil -- error: imported globals are const
do -- explicit global variable will not be imported import global global FLAG print FLAG FLAG = 123Export
Section titled “Export”The export statement offers a concise way to define modules.
Named Export
Section titled “Named Export”Named export will define a local variable as well as adding a field in the exported table.
export a, b, c = 1, 2, 3export cool = "cat"
export What = if this "abc"else "def"
export y = -> hallo = 3434
export class Something umm: "cool"export a, b, c = 1, 2, 3export cool = "cat"
export What = if this "abc"else "def"
export y = -> hallo = 3434
export class Something umm: "cool"Doing named export with destructuring.
export :loadstring, to_lua: tolua = yueexport {itemA: {:fieldA = 'default'}} = tbexport :loadstring, to_lua: tolua = yueexport {itemA: {:fieldA = 'default'}} = tbExport named items from module without creating local variables.
export.itemA = tbexport.<index> = itemsexport["a-b-c"] = 123export.itemA = tbexport.<index> = itemsexport["a-b-c"] = 123Unnamed Export
Section titled “Unnamed Export”Unnamed export will add the target item into the array part of the exported table.
d, e, f = 3, 2, 1export d, e, f
export if this 123else 456
export with tmp j = 2000d, e, f = 3, 2, 1export d, e, f
export if this 123else 456
export with tmp j = 2000Default Export
Section titled “Default Export”Using the default keyword in export statement to replace the exported table with any thing.
export default -> print "hello" 123export default -> print "hello" 123