From 873aced12cf65e633c95358f89a1d5ec37b81d00 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 15 Jan 2026 17:45:33 +0800 Subject: Updated docs. [skip CI] --- doc/docs/doc/README.md | 93 ++++++++++++++++++++++++++++------------------- doc/docs/zh/doc/README.md | 93 ++++++++++++++++++++++++++++------------------- 2 files changed, 112 insertions(+), 74 deletions(-) diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index bda82a6..d862777 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md @@ -928,43 +928,6 @@ tb = 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. -#### Import Global - -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 safely assign to them. - -```moonscript -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 = 123 -``` - -
-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 = 123
-
-
- ```moonscript -- used as table destructuring do @@ -1016,6 +979,62 @@ do +### 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. + +```moonscript +do + import tostring + import table.concat + print concat ["a", tostring 1] +``` + +
+do
+  import tostring
+  import table.concat
+  print concat ["a", tostring 1]
+
+
+ +#### Automatic 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. + +```moonscript +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 = 123 +``` + +
+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 = 123
+
+
+ ### Export The export statement offers a concise way to define modules. diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md index d316657..786892d 100755 --- a/doc/docs/zh/doc/README.md +++ b/doc/docs/zh/doc/README.md @@ -926,43 +926,6 @@ tb = 导入语句是一个语法糖,用于需要引入一个模块或者从已导入的模块中提取子项目。从模块导入的变量默认为不可修改的常量。 -#### 导入全局变量 - -在代码块顶部写 `import global`,会将当前作用域中尚未显式声明或赋值过的变量名,自动导入为本地常量,并在该语句的位置绑定到同名的全局变量。 - -但是在同一作用域中被显式声明为全局的变量不会被自动导入,因此可以继续进行赋值操作。 - -```moonscript -do - import global - print "hello" - math.random 3 - -- print = nil -- 报错:自动导入的全局变量为常量 - -do - -- 被显式声明为全局的变量不会被自动导入 - import global - global FLAG - print FLAG - FLAG = 123 -``` - -
-do
-  import global
-  print "hello"
-  math.random 3
-  -- print = nil -- 报错:自动导入的全局变量是常量
-
-do
-  -- 被显式声明为全局的变量不会被自动导入
-  import global
-  global FLAG
-  print FLAG
-  FLAG = 123
-
-
- ```moonscript -- 用作表解构 do @@ -1014,6 +977,62 @@ do +### 导入全局变量 + +你可以使用 `import` 将指定的全局变量导入到本地变量中。当导入一系列对全局变量的链式访问时,最后一个访问的字段将被赋值给本地变量。 + +```moonscript +do + import tostring + import table.concat + print concat ["a", tostring 1] +``` + +
+do
+  import tostring
+  import table.concat
+  print concat ["a", tostring 1]
+
+
+ +#### 自动导入 + +在一个代码块的顶部写 `import global`,会将当前作用域中尚未显式声明或赋值过的变量名,自动导入为本地常量,并在该语句的位置绑定到同名的全局变量。 + +但是在同一作用域中被显式声明为全局的变量不会被自动导入,因此可以继续进行赋值操作。 + +```moonscript +do + import global + print "hello" + math.random 3 + -- print = nil -- 报错:自动导入的全局变量为常量 + +do + -- 被显式声明为全局的变量不会被自动导入 + import global + global FLAG + print FLAG + FLAG = 123 +``` + +
+do
+  import global
+  print "hello"
+  math.random 3
+  -- print = nil -- 报错:自动导入的全局变量是常量
+
+do
+  -- 被显式声明为全局的变量不会被自动导入
+  import global
+  global FLAG
+  print FLAG
+  FLAG = 123
+
+
+ ### 导出 导出语句提供了一种简洁的方式来定义当前的模块。 -- cgit v1.2.3-55-g6feb