aboutsummaryrefslogtreecommitdiff
path: root/doc/docs
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2026-01-15 17:07:11 +0800
committerLi Jin <dragon-fly@qq.com>2026-01-15 17:07:11 +0800
commitfced0c4f4101ad7c8d81432a0e8c45d38b72616c (patch)
treeed673461c7ef3c614cb5d56905c437f6a6b27454 /doc/docs
parent4177d237e3ed642b2bba5bec13127a44d2b0524d (diff)
downloadyuescript-fced0c4f4101ad7c8d81432a0e8c45d38b72616c.tar.gz
yuescript-fced0c4f4101ad7c8d81432a0e8c45d38b72616c.tar.bz2
yuescript-fced0c4f4101ad7c8d81432a0e8c45d38b72616c.zip
Added `import global` syntax.
Diffstat (limited to 'doc/docs')
-rwxr-xr-xdoc/docs/doc/README.md37
-rwxr-xr-xdoc/docs/zh/doc/README.md37
2 files changed, 74 insertions, 0 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md
index 5308ebc..bda82a6 100755
--- a/doc/docs/doc/README.md
+++ b/doc/docs/doc/README.md
@@ -928,6 +928,43 @@ 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
931```moonscript 968```moonscript
932-- used as table destructuring 969-- used as table destructuring
933do 970do
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md
index de5dff1..d316657 100755
--- a/doc/docs/zh/doc/README.md
+++ b/doc/docs/zh/doc/README.md
@@ -926,6 +926,43 @@ 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
929```moonscript 966```moonscript
930-- 用作表解构 967-- 用作表解构
931do 968do