Module
Die import-Anweisung ist syntaktischer Zucker für require und hilft beim Extrahieren von Einträgen aus importierten Modulen. Importierte Elemente sind standardmäßig const.
-- als Tabellen-Destrukturierungdo import insert, concat from table -- Fehler beim Zuweisen zu insert, concat import C, Ct, Cmt from require "lpeg" -- Kurzform für implizites Require import x, y, z from 'mymodule' -- Import im Python-Stil from 'module' import a, b, c
-- Kurzform zum Laden eines Modulsdo import 'module' import 'module_x' import "d-a-s-h-e-s" import "module.part"
-- Modul mit Alias oder Tabellen-Destrukturierung ladendo import "player" as PlayerModule import "lpeg" as :C, :Ct, :Cmt import "export" as {one, two, Something:{umm:{ch}}}-- als Tabellen-Destrukturierungdo import insert, concat from table -- Fehler beim Zuweisen zu insert, concat import C, Ct, Cmt from require "lpeg" -- Kurzform für implizites Require import x, y, z from 'mymodule' -- Import im Python-Stil from 'module' import a, b, c
-- Kurzform zum Laden eines Modulsdo import 'module' import 'module_x' import "d-a-s-h-e-s" import "module.part"
-- Modul mit Alias oder Tabellen-Destrukturierung ladendo import "player" as PlayerModule import "lpeg" as :C, :Ct, :Cmt import "export" as {one, two, Something:{umm:{ch}}}Import von Globals
Abschnitt betitelt „Import von Globals“Du kannst mit import bestimmte Globals in lokale Variablen importieren. Wenn du eine Kette von Globalzugriffen importierst, wird das letzte Feld der lokalen Variable zugewiesen.
do import tostring import table.concat print concat ["a", tostring 1]do import tostring import table.concat print concat ["a", tostring 1]Automatischer Global-Import
Abschnitt betitelt „Automatischer Global-Import“Du kannst import global am Anfang eines Blocks platzieren, um automatisch alle Namen zu importieren, die im aktuellen Scope nicht explizit deklariert oder zugewiesen sind. Diese impliziten Importe werden als lokale const behandelt, die an die entsprechenden Globals zum Zeitpunkt der Anweisung gebunden sind.
Namen, die im selben Scope explizit als global deklariert werden, werden nicht importiert, sodass du sie weiterhin zuweisen kannst.
do import global print "hallo" math.random 3 -- print = nil -- Fehler: importierte Globals sind const
do -- explizite globale Variable wird nicht importiert import global global FLAG print FLAG FLAG = 123do import global print "hallo" math.random 3 -- print = nil -- Fehler: importierte Globals sind const
do -- explizite globale Variable wird nicht importiert import global global FLAG print FLAG FLAG = 123Die export-Anweisung bietet eine knappe Möglichkeit, Module zu definieren.
Benannter Export
Abschnitt betitelt „Benannter Export“Benannter Export definiert eine lokale Variable und fügt ein Feld in die exportierte Tabelle ein.
export a, b, c = 1, 2, 3export cool = "Katze"
export What = if this "abc"else "def"
export y = -> hallo = 3434
export class Something umm: "cool"export a, b, c = 1, 2, 3export cool = "Katze"
export What = if this "abc"else "def"
export y = -> hallo = 3434
export class Something umm: "cool"Benannter Export mit Destructuring.
export :loadstring, to_lua: tolua = yueexport {itemA: {:fieldA = 'default'}} = tbexport :loadstring, to_lua: tolua = yueexport {itemA: {:fieldA = 'default'}} = tbBenannte Elemente aus dem Modul exportieren, ohne lokale Variablen zu erstellen.
export.itemA = tbexport.<index> = itemsexport["a-b-c"] = 123export.itemA = tbexport.<index> = itemsexport["a-b-c"] = 123Unbenannter Export
Abschnitt betitelt „Unbenannter Export“Unbenannter Export fügt das Ziel-Element in den Array-Teil der exportierten Tabelle ein.
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
Abschnitt betitelt „Default-Export“Mit dem Schlüsselwort default in einer export-Anweisung wird die exportierte Tabelle durch ein beliebiges Objekt ersetzt.
export default -> print "hallo" 123export default -> print "hallo" 123