diff options
Diffstat (limited to 'doc/docs/doc/README.md')
| -rwxr-xr-x | doc/docs/doc/README.md | 93 |
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 | ||
| 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. | 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 | ||
| 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 | |||
| 968 | ```moonscript | 931 | ```moonscript |
| 969 | -- used as table destructuring | 932 | -- used as table destructuring |
| 970 | do | 933 | do |
| @@ -1016,6 +979,62 @@ do | |||
| 1016 | </pre> | 979 | </pre> |
| 1017 | </YueDisplay> | 980 | </YueDisplay> |
| 1018 | 981 | ||
| 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 | |||
| 1019 | ### Export | 1038 | ### Export |
| 1020 | 1039 | ||
| 1021 | The export statement offers a concise way to define modules. | 1040 | The export statement offers a concise way to define modules. |
