Modul
Import
Section titled “Import”Pernyataan import adalah sintaks sugar untuk me-require modul atau membantu mengekstrak item dari modul yang diimpor. Item yang diimpor bersifat const secara default.
-- digunakan sebagai destrukturisasi tabeldo import insert, concat from table -- akan error saat meng-assign ke insert, concat import C, Ct, Cmt from require "lpeg" -- shortcut untuk require implisit import x, y, z from 'mymodule' -- import gaya Python from 'module' import a, b, c
-- shortcut untuk require moduldo import 'module' import 'module_x' import "d-a-s-h-e-s" import "module.part"
-- require modul dengan aliasing atau destrukturisasi tabeldo import "player" as PlayerModule import "lpeg" as :C, :Ct, :Cmt import "export" as {one, two, Something:{umm:{ch}}}-- digunakan sebagai destrukturisasi tabeldo import insert, concat from table -- akan error saat meng-assign ke insert, concat import C, Ct, Cmt from require "lpeg" -- shortcut untuk require implisit import x, y, z from 'mymodule' -- import gaya Python from 'module' import a, b, c
-- shortcut untuk require moduldo import 'module' import 'module_x' import "d-a-s-h-e-s" import "module.part"
-- require modul dengan aliasing atau destrukturisasi tabeldo import "player" as PlayerModule import "lpeg" as :C, :Ct, :Cmt import "export" as {one, two, Something:{umm:{ch}}}Import Global
Section titled “Import Global”Anda dapat mengimpor global tertentu ke variabel local dengan import. Saat mengimpor rangkaian akses variabel global, field terakhir akan di-assign ke variabel local.
do import tostring import table.concat print concat ["a", tostring 1]do import tostring import table.concat print concat ["a", tostring 1]Import Variabel Global Otomatis
Section titled “Import Variabel Global Otomatis”Anda dapat menempatkan import global di awal blok untuk mengimpor secara otomatis semua nama yang belum dideklarasikan atau di-assign secara eksplisit di scope saat ini sebagai global. Import implisit ini diperlakukan sebagai local const yang mereferensikan global terkait pada posisi pernyataan tersebut.
Nama yang secara eksplisit dideklarasikan sebagai global di scope yang sama tidak akan diimpor, sehingga Anda masih bisa meng-assign ke mereka.
do import global print "hello" math.random 3 -- print = nil -- error: imported globals are const
do -- variabel global eksplisit tidak akan diimpor import global global FLAG print FLAG FLAG = 123do import global print "hello" math.random 3 -- print = nil -- error: imported globals are const
do -- variabel global eksplisit tidak akan diimpor import global global FLAG print FLAG FLAG = 123Export
Section titled “Export”Pernyataan export menawarkan cara ringkas untuk mendefinisikan modul.
Export Bernama
Section titled “Export Bernama”Export bernama akan mendefinisikan variabel local sekaligus menambahkan field di tabel export.
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"Melakukan export bernama dengan destrukturisasi.
export :loadstring, to_lua: tolua = yueexport {itemA: {:fieldA = 'default'}} = tbexport :loadstring, to_lua: tolua = yueexport {itemA: {:fieldA = 'default'}} = tbExport item bernama dari modul tanpa membuat variabel local.
export.itemA = tbexport.<index> = itemsexport["a-b-c"] = 123export.itemA = tbexport.<index> = itemsexport["a-b-c"] = 123Export Tanpa Nama
Section titled “Export Tanpa Nama”Export tanpa nama akan menambahkan item target ke bagian array dari tabel export.
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 = 2000Export Default
Section titled “Export Default”Gunakan kata kunci default dalam pernyataan export untuk mengganti tabel export dengan apa pun.
export default -> print "hello" 123export default -> print "hello" 123