You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/docs/doc/README.md
+37Lines changed: 37 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -928,6 +928,43 @@ tb =
928
928
929
929
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.
930
930
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
0 commit comments