aboutsummaryrefslogtreecommitdiff
path: root/doc/docs/doc/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'doc/docs/doc/README.md')
-rwxr-xr-xdoc/docs/doc/README.md93
1 files changed, 56 insertions, 37 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.