aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2026-01-15 17:45:33 +0800
committerLi Jin <dragon-fly@qq.com>2026-01-15 17:45:33 +0800
commit873aced12cf65e633c95358f89a1d5ec37b81d00 (patch)
tree00a51f8e047c75f57909219d4dfdbba795c23ccb
parent079f563e63d2d12db92db9226e248d5d5f064e3d (diff)
downloadyuescript-873aced12cf65e633c95358f89a1d5ec37b81d00.tar.gz
yuescript-873aced12cf65e633c95358f89a1d5ec37b81d00.tar.bz2
yuescript-873aced12cf65e633c95358f89a1d5ec37b81d00.zip
Updated docs. [skip CI]
-rwxr-xr-xdoc/docs/doc/README.md93
-rwxr-xr-xdoc/docs/zh/doc/README.md93
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 =
928 928
929The 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. 929The 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.
930 930
931#### Import Global
932
933You 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.
934
935Names that are explicitly declared as globals in the same scope will not be imported, so you can safely assign to them.
936
937```moonscript
938do
939 import global
940 print "hello"
941 math.random 3
942 -- print = nil -- error: imported globals are const
943
944do
945 -- explicit global variable will not be imported
946 import global
947 global FLAG
948 print FLAG
949 FLAG = 123
950```
951<YueDisplay>
952<pre>
953do
954 import global
955 print "hello"
956 math.random 3
957 -- print = nil -- error: imported globals are const
958
959do
960 -- explicit global variable will not be imported
961 import global
962 global FLAG
963 print FLAG
964 FLAG = 123
965</pre>
966</YueDisplay>
967
968```moonscript 931```moonscript
969-- used as table destructuring 932-- used as table destructuring
970do 933do
@@ -1016,6 +979,62 @@ do
1016</pre> 979</pre>
1017</YueDisplay> 980</YueDisplay>
1018 981
982### Import Global
983
984You 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.
985
986```moonscript
987do
988 import tostring
989 import table.concat
990 print concat ["a", tostring 1]
991```
992<YueDisplay>
993<pre>
994do
995 import tostring
996 import table.concat
997 print concat ["a", tostring 1]
998</pre>
999</YueDisplay>
1000
1001#### Automatic Import
1002
1003You 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.
1004
1005Names that are explicitly declared as globals in the same scope will not be imported, so you can still assign to them.
1006
1007```moonscript
1008do
1009 import global
1010 print "hello"
1011 math.random 3
1012 -- print = nil -- error: imported globals are const
1013
1014do
1015 -- explicit global variable will not be imported
1016 import global
1017 global FLAG
1018 print FLAG
1019 FLAG = 123
1020```
1021<YueDisplay>
1022<pre>
1023do
1024 import global
1025 print "hello"
1026 math.random 3
1027 -- print = nil -- error: imported globals are const
1028
1029do
1030 -- explicit global variable will not be imported
1031 import global
1032 global FLAG
1033 print FLAG
1034 FLAG = 123
1035</pre>
1036</YueDisplay>
1037
1019### Export 1038### Export
1020 1039
1021The export statement offers a concise way to define modules. 1040The 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 =
926 926
927导入语句是一个语法糖,用于需要引入一个模块或者从已导入的模块中提取子项目。从模块导入的变量默认为不可修改的常量。 927导入语句是一个语法糖,用于需要引入一个模块或者从已导入的模块中提取子项目。从模块导入的变量默认为不可修改的常量。
928 928
929#### 导入全局变量
930
931在代码块顶部写 `import global`,会将当前作用域中尚未显式声明或赋值过的变量名,自动导入为本地常量,并在该语句的位置绑定到同名的全局变量。
932
933但是在同一作用域中被显式声明为全局的变量不会被自动导入,因此可以继续进行赋值操作。
934
935```moonscript
936do
937 import global
938 print "hello"
939 math.random 3
940 -- print = nil -- 报错:自动导入的全局变量为常量
941
942do
943 -- 被显式声明为全局的变量不会被自动导入
944 import global
945 global FLAG
946 print FLAG
947 FLAG = 123
948```
949<YueDisplay>
950<pre>
951do
952 import global
953 print "hello"
954 math.random 3
955 -- print = nil -- 报错:自动导入的全局变量是常量
956
957do
958 -- 被显式声明为全局的变量不会被自动导入
959 import global
960 global FLAG
961 print FLAG
962 FLAG = 123
963</pre>
964</YueDisplay>
965
966```moonscript 929```moonscript
967-- 用作表解构 930-- 用作表解构
968do 931do
@@ -1014,6 +977,62 @@ do
1014</pre> 977</pre>
1015</YueDisplay> 978</YueDisplay>
1016 979
980### 导入全局变量
981
982你可以使用 `import` 将指定的全局变量导入到本地变量中。当导入一系列对全局变量的链式访问时,最后一个访问的字段将被赋值给本地变量。
983
984```moonscript
985do
986 import tostring
987 import table.concat
988 print concat ["a", tostring 1]
989```
990<YueDisplay>
991<pre>
992do
993 import tostring
994 import table.concat
995 print concat ["a", tostring 1]
996</pre>
997</YueDisplay>
998
999#### 自动导入
1000
1001在一个代码块的顶部写 `import global`,会将当前作用域中尚未显式声明或赋值过的变量名,自动导入为本地常量,并在该语句的位置绑定到同名的全局变量。
1002
1003但是在同一作用域中被显式声明为全局的变量不会被自动导入,因此可以继续进行赋值操作。
1004
1005```moonscript
1006do
1007 import global
1008 print "hello"
1009 math.random 3
1010 -- print = nil -- 报错:自动导入的全局变量为常量
1011
1012do
1013 -- 被显式声明为全局的变量不会被自动导入
1014 import global
1015 global FLAG
1016 print FLAG
1017 FLAG = 123
1018```
1019<YueDisplay>
1020<pre>
1021do
1022 import global
1023 print "hello"
1024 math.random 3
1025 -- print = nil -- 报错:自动导入的全局变量是常量
1026
1027do
1028 -- 被显式声明为全局的变量不会被自动导入
1029 import global
1030 global FLAG
1031 print FLAG
1032 FLAG = 123
1033</pre>
1034</YueDisplay>
1035
1017### 导出 1036### 导出
1018 1037
1019导出语句提供了一种简洁的方式来定义当前的模块。 1038导出语句提供了一种简洁的方式来定义当前的模块。