1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
# Module
## Import
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.
```yuescript
-- used as table destructuring
do
import insert, concat from table
-- report error when assigning to insert, concat
import C, Ct, Cmt from require "lpeg"
-- shortcut for implicit requiring
import x, y, z from 'mymodule'
-- import with Python style
from 'module' import a, b, c
-- shortcut for requring a module
do
import 'module'
import 'module_x'
import "d-a-s-h-e-s"
import "module.part"
-- requring module with aliasing or table destructuring
do
import "player" as PlayerModule
import "lpeg" as :C, :Ct, :Cmt
import "export" as {one, two, Something:{umm:{ch}}}
```
<YueDisplay>
```yue
-- used as table destructuring
do
import insert, concat from table
-- report error when assigning to insert, concat
import C, Ct, Cmt from require "lpeg"
-- shortcut for implicit requiring
import x, y, z from 'mymodule'
-- import with Python style
from 'module' import a, b, c
-- shortcut for requring a module
do
import 'module'
import 'module_x'
import "d-a-s-h-e-s"
import "module.part"
-- requring module with aliasing or table destructuring
do
import "player" as PlayerModule
import "lpeg" as :C, :Ct, :Cmt
import "export" as {one, two, Something:{umm:{ch}}}
```
</YueDisplay>
## Import Global
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.
```yuescript
do
import tostring
import table.concat
print concat ["a", tostring 1]
```
<YueDisplay>
```yue
do
import tostring
import table.concat
print concat ["a", tostring 1]
```
</YueDisplay>
### Automatic Global Variable Import
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.
Names that are explicitly declared as globals in the same scope will not be imported, so you can still assign to them.
```yuescript
do
import global
print "hello"
math.random 3
-- print = nil -- error: imported globals are const
do
-- explicit global variable will not be imported
import global
global FLAG
print FLAG
FLAG = 123
```
<YueDisplay>
```yue
do
import global
print "hello"
math.random 3
-- print = nil -- error: imported globals are const
do
-- explicit global variable will not be imported
import global
global FLAG
print FLAG
FLAG = 123
```
</YueDisplay>
## Export
The export statement offers a concise way to define modules.
### Named Export
Named export will define a local variable as well as adding a field in the exported table.
```yuescript
export a, b, c = 1, 2, 3
export cool = "cat"
export What = if this
"abc"
else
"def"
export y = ->
hallo = 3434
export class Something
umm: "cool"
```
<YueDisplay>
```yue
export a, b, c = 1, 2, 3
export cool = "cat"
export What = if this
"abc"
else
"def"
export y = ->
hallo = 3434
export class Something
umm: "cool"
```
</YueDisplay>
Doing named export with destructuring.
```yuescript
export :loadstring, to_lua: tolua = yue
export {itemA: {:fieldA = 'default'}} = tb
```
<YueDisplay>
```yue
export :loadstring, to_lua: tolua = yue
export {itemA: {:fieldA = 'default'}} = tb
```
</YueDisplay>
Export named items from module without creating local variables.
```yuescript
export.itemA = tb
export.<index> = items
export["a-b-c"] = 123
```
<YueDisplay>
```yue
export.itemA = tb
export.<index> = items
export["a-b-c"] = 123
```
</YueDisplay>
### Unnamed Export
Unnamed export will add the target item into the array part of the exported table.
```yuescript
d, e, f = 3, 2, 1
export d, e, f
export if this
123
else
456
export with tmp
j = 2000
```
<YueDisplay>
```yue
d, e, f = 3, 2, 1
export d, e, f
export if this
123
else
456
export with tmp
j = 2000
```
</YueDisplay>
### Default Export
Using the **default** keyword in export statement to replace the exported table with any thing.
```yuescript
export default ->
print "hello"
123
```
<YueDisplay>
```yue
export default ->
print "hello"
123
```
</YueDisplay>
|