Skip to content

Commit 873aced

Browse files
committed
Updated docs. [skip CI]
1 parent 079f563 commit 873aced

2 files changed

Lines changed: 112 additions & 74 deletions

File tree

doc/docs/doc/README.md

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -928,43 +928,6 @@ tb =
928928

929929
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.
930930

931-
#### Import Global
932-
933-
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.
934-
935-
Names that are explicitly declared as globals in the same scope will not be imported, so you can safely assign to them.
936-
937-
```moonscript
938-
do
939-
import global
940-
print "hello"
941-
math.random 3
942-
-- print = nil -- error: imported globals are const
943-
944-
do
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>
953-
do
954-
import global
955-
print "hello"
956-
math.random 3
957-
-- print = nil -- error: imported globals are const
958-
959-
do
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-
968931
```moonscript
969932
-- used as table destructuring
970933
do
@@ -1016,6 +979,62 @@ do
1016979
</pre>
1017980
</YueDisplay>
1018981

982+
### Import Global
983+
984+
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.
985+
986+
```moonscript
987+
do
988+
import tostring
989+
import table.concat
990+
print concat ["a", tostring 1]
991+
```
992+
<YueDisplay>
993+
<pre>
994+
do
995+
import tostring
996+
import table.concat
997+
print concat ["a", tostring 1]
998+
</pre>
999+
</YueDisplay>
1000+
1001+
#### Automatic Import
1002+
1003+
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.
1004+
1005+
Names that are explicitly declared as globals in the same scope will not be imported, so you can still assign to them.
1006+
1007+
```moonscript
1008+
do
1009+
import global
1010+
print "hello"
1011+
math.random 3
1012+
-- print = nil -- error: imported globals are const
1013+
1014+
do
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>
1023+
do
1024+
import global
1025+
print "hello"
1026+
math.random 3
1027+
-- print = nil -- error: imported globals are const
1028+
1029+
do
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+
10191038
### Export
10201039

10211040
The export statement offers a concise way to define modules.

doc/docs/zh/doc/README.md

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -926,43 +926,6 @@ tb =
926926

927927
导入语句是一个语法糖,用于需要引入一个模块或者从已导入的模块中提取子项目。从模块导入的变量默认为不可修改的常量。
928928

929-
#### 导入全局变量
930-
931-
在代码块顶部写 `import global`,会将当前作用域中尚未显式声明或赋值过的变量名,自动导入为本地常量,并在该语句的位置绑定到同名的全局变量。
932-
933-
但是在同一作用域中被显式声明为全局的变量不会被自动导入,因此可以继续进行赋值操作。
934-
935-
```moonscript
936-
do
937-
import global
938-
print "hello"
939-
math.random 3
940-
-- print = nil -- 报错:自动导入的全局变量为常量
941-
942-
do
943-
-- 被显式声明为全局的变量不会被自动导入
944-
import global
945-
global FLAG
946-
print FLAG
947-
FLAG = 123
948-
```
949-
<YueDisplay>
950-
<pre>
951-
do
952-
import global
953-
print "hello"
954-
math.random 3
955-
-- print = nil -- 报错:自动导入的全局变量是常量
956-
957-
do
958-
-- 被显式声明为全局的变量不会被自动导入
959-
import global
960-
global FLAG
961-
print FLAG
962-
FLAG = 123
963-
</pre>
964-
</YueDisplay>
965-
966929
```moonscript
967930
-- 用作表解构
968931
do
@@ -1014,6 +977,62 @@ do
1014977
</pre>
1015978
</YueDisplay>
1016979

980+
### 导入全局变量
981+
982+
你可以使用 `import` 将指定的全局变量导入到本地变量中。当导入一系列对全局变量的链式访问时,最后一个访问的字段将被赋值给本地变量。
983+
984+
```moonscript
985+
do
986+
import tostring
987+
import table.concat
988+
print concat ["a", tostring 1]
989+
```
990+
<YueDisplay>
991+
<pre>
992+
do
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
1006+
do
1007+
import global
1008+
print "hello"
1009+
math.random 3
1010+
-- print = nil -- 报错:自动导入的全局变量为常量
1011+
1012+
do
1013+
-- 被显式声明为全局的变量不会被自动导入
1014+
import global
1015+
global FLAG
1016+
print FLAG
1017+
FLAG = 123
1018+
```
1019+
<YueDisplay>
1020+
<pre>
1021+
do
1022+
import global
1023+
print "hello"
1024+
math.random 3
1025+
-- print = nil -- 报错:自动导入的全局变量是常量
1026+
1027+
do
1028+
-- 被显式声明为全局的变量不会被自动导入
1029+
import global
1030+
global FLAG
1031+
print FLAG
1032+
FLAG = 123
1033+
</pre>
1034+
</YueDisplay>
1035+
10171036
### 导出
10181037

10191038
导出语句提供了一种简洁的方式来定义当前的模块。

0 commit comments

Comments
 (0)