diff options
author | Li Jin <dragon-fly@qq.com> | 2025-06-04 16:43:26 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2025-06-04 16:43:26 +0800 |
commit | a575de6992cabe0b809e363391563841d6213d4e (patch) | |
tree | d7a711e65b114d6a2a87988c1e611b1583bc1d45 /doc/docs | |
parent | 0e72454a11f65d9ac800dedb698ddfcc15933785 (diff) | |
download | yuescript-a575de6992cabe0b809e363391563841d6213d4e.tar.gz yuescript-a575de6992cabe0b809e363391563841d6213d4e.tar.bz2 yuescript-a575de6992cabe0b809e363391563841d6213d4e.zip |
Updated docs. [skip CI]
Diffstat (limited to 'doc/docs')
-rwxr-xr-x | doc/docs/doc/README.md | 121 | ||||
-rwxr-xr-x | doc/docs/zh/doc/README.md | 125 |
2 files changed, 244 insertions, 2 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index c0312f7..9f7d969 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md | |||
@@ -586,6 +586,21 @@ merge = {...a, ...b} | |||
586 | </pre> | 586 | </pre> |
587 | </YueDisplay> | 587 | </YueDisplay> |
588 | 588 | ||
589 | ### Table Reversed Indexing | ||
590 | |||
591 | You can use the **#** operator to get the last elements of a table. | ||
592 | |||
593 | ```moonscript | ||
594 | print data.items[#] -- get the last element of a table | ||
595 | print data.items[#-1] -- get the second last element of a table | ||
596 | ``` | ||
597 | <YueDisplay> | ||
598 | <pre> | ||
599 | print data.items[#] -- get the last element of a table | ||
600 | print data.items[#-1] -- get the second last element of a table | ||
601 | </pre> | ||
602 | </YueDisplay> | ||
603 | |||
589 | ### Metatable | 604 | ### Metatable |
590 | 605 | ||
591 | The **<>** operator can be used as a shortcut for metatable manipulation. | 606 | The **<>** operator can be used as a shortcut for metatable manipulation. |
@@ -1287,6 +1302,52 @@ You can use `_` as placeholder when doing a list destructuring: | |||
1287 | </pre> | 1302 | </pre> |
1288 | </YueDisplay> | 1303 | </YueDisplay> |
1289 | 1304 | ||
1305 | ### Range Destructuring | ||
1306 | |||
1307 | You can use the spread operator `...` in list destructuring to capture a range of values. This is useful when you want to extract specific elements from the beginning and end of a list while collecting the rest in between. | ||
1308 | |||
1309 | ```moonscript | ||
1310 | orders = ["first", "second", "third", "fourth", "last"] | ||
1311 | [first, ...bulk, last] = orders | ||
1312 | print first -- prints: first | ||
1313 | print bulk -- prints: {"second", "third", "fourth"} | ||
1314 | print last -- prints: last | ||
1315 | ``` | ||
1316 | <YueDisplay> | ||
1317 | <pre> | ||
1318 | orders = ["first", "second", "third", "fourth", "last"] | ||
1319 | [first, ...bulk, last] = orders | ||
1320 | print first -- prints: first | ||
1321 | print bulk -- prints: {"second", "third", "fourth"} | ||
1322 | print last -- prints: last | ||
1323 | </pre> | ||
1324 | </YueDisplay> | ||
1325 | |||
1326 | The spread operator can be used in different positions to capture different ranges, and you can use `_` as a placeholder for the values you don't want to capture: | ||
1327 | |||
1328 | ```moonscript | ||
1329 | -- Capture everything after first element | ||
1330 | [first, ...rest] = orders | ||
1331 | |||
1332 | -- Capture everything before last element | ||
1333 | [...start, last] = orders | ||
1334 | |||
1335 | -- Capture things except the middle elements | ||
1336 | [first, _..., last] = orders | ||
1337 | ``` | ||
1338 | <YueDisplay> | ||
1339 | <pre> | ||
1340 | -- Capture everything after first element | ||
1341 | [first, ...rest] = orders | ||
1342 | |||
1343 | -- Capture everything before last element | ||
1344 | [...start, last] = orders | ||
1345 | |||
1346 | -- Capture things except the middle elements | ||
1347 | [first, _..., last] = orders | ||
1348 | </pre> | ||
1349 | </YueDisplay> | ||
1350 | |||
1290 | ### Destructuring In Other Places | 1351 | ### Destructuring In Other Places |
1291 | 1352 | ||
1292 | Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop: | 1353 | Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop: |
@@ -2344,6 +2405,45 @@ slice = [item for item in *items[,,2]] | |||
2344 | </pre> | 2405 | </pre> |
2345 | </YueDisplay> | 2406 | </YueDisplay> |
2346 | 2407 | ||
2408 | Both the minimum and maximum bounds can be negative, which means that the bounds are counted from the end of the table. | ||
2409 | |||
2410 | ```moonscript | ||
2411 | -- take the last 4 items | ||
2412 | slice = [item for item in *items[-4,-1]] | ||
2413 | ``` | ||
2414 | <YueDisplay> | ||
2415 | <pre> | ||
2416 | -- take the last 4 items | ||
2417 | slice = [item for item in *items[-4,-1]] | ||
2418 | </pre> | ||
2419 | </YueDisplay> | ||
2420 | |||
2421 | The step size can also be negative, which means that the items are taken in reverse order. | ||
2422 | |||
2423 | ```moonscript | ||
2424 | reverse_slice = [item for item in *items[-1,1,-1]] | ||
2425 | ``` | ||
2426 | <YueDisplay> | ||
2427 | <pre> | ||
2428 | reverse_slice = [item for item in *items[-1,1,-1]] | ||
2429 | </pre> | ||
2430 | </YueDisplay> | ||
2431 | |||
2432 | #### Slicing Expression | ||
2433 | |||
2434 | Slicing can also be used as an expression. This is useful for getting a sub-list of a table. | ||
2435 | |||
2436 | ```moonscript | ||
2437 | -- take the 2nd and 4th items as a new list | ||
2438 | sub_list = items[2, 4] | ||
2439 | ``` | ||
2440 | <YueDisplay> | ||
2441 | <pre> | ||
2442 | -- take the 2nd and 4th items as a new list | ||
2443 | sub_list = items[2, 4] | ||
2444 | </pre> | ||
2445 | </YueDisplay> | ||
2446 | |||
2347 | ## For Loop | 2447 | ## For Loop |
2348 | 2448 | ||
2349 | There are two for loop forms, just like in Lua. A numeric one and a generic one: | 2449 | There are two for loop forms, just like in Lua. A numeric one and a generic one: |
@@ -2989,6 +3089,27 @@ switch tb | |||
2989 | </pre> | 3089 | </pre> |
2990 | </YueDisplay> | 3090 | </YueDisplay> |
2991 | 3091 | ||
3092 | Match against a list and capture a range of elements. | ||
3093 | |||
3094 | ```moonscript | ||
3095 | segments = ["admin", "users", "logs", "view"] | ||
3096 | switch segments | ||
3097 | when [...groups, resource, action] | ||
3098 | print "Group:", groups -- prints: {"admin", "users"} | ||
3099 | print "Resource:", resource -- prints: "logs" | ||
3100 | print "Action:", action -- prints: "view" | ||
3101 | ``` | ||
3102 | <YueDisplay> | ||
3103 | <pre> | ||
3104 | segments = ["admin", "users", "logs", "view"] | ||
3105 | switch segments | ||
3106 | when [...groups, resource, action] | ||
3107 | print "Group:", groups -- prints: {"admin", "users"} | ||
3108 | print "Resource:", resource -- prints: "logs" | ||
3109 | print "Action:", action -- prints: "view" | ||
3110 | </pre> | ||
3111 | </YueDisplay> | ||
3112 | |||
2992 | ## Object Oriented Programming | 3113 | ## Object Oriented Programming |
2993 | 3114 | ||
2994 | In these examples, the generated Lua code may appear overwhelming. It is best to focus on the meaning of the YueScript code at first, then look into the Lua code if you wish to know the implementation details. | 3115 | In these examples, the generated Lua code may appear overwhelming. It is best to focus on the meaning of the YueScript code at first, then look into the Lua code if you wish to know the implementation details. |
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md index b4e594c..4152591 100755 --- a/doc/docs/zh/doc/README.md +++ b/doc/docs/zh/doc/README.md | |||
@@ -585,6 +585,21 @@ merge = {...a, ...b} | |||
585 | </pre> | 585 | </pre> |
586 | </YueDisplay> | 586 | </YueDisplay> |
587 | 587 | ||
588 | ### 表反向索引 | ||
589 | |||
590 | 你可以使用 **#** 操作符来反向索引表中的元素。 | ||
591 | |||
592 | ```moonscript | ||
593 | print data.items[#] -- 获取表的最后一个元素 | ||
594 | print data.items[#-1] -- 获取表的倒数第二个元素 | ||
595 | ``` | ||
596 | <YueDisplay> | ||
597 | <pre> | ||
598 | print data.items[#] -- 获取表的最后一个元素 | ||
599 | print data.items[#-1] -- 获取表的倒数第二个元素 | ||
600 | </pre> | ||
601 | </YueDisplay> | ||
602 | |||
588 | ### 元表 | 603 | ### 元表 |
589 | 604 | ||
590 | **<>** 操作符可提供元表操作的快捷方式。 | 605 | **<>** 操作符可提供元表操作的快捷方式。 |
@@ -1285,9 +1300,55 @@ print first, second, color | |||
1285 | </pre> | 1300 | </pre> |
1286 | </YueDisplay> | 1301 | </YueDisplay> |
1287 | 1302 | ||
1288 | ### 在其它地方的解构 | 1303 | ### 范围解构 |
1304 | |||
1305 | 你可以使用展开运算符 `...` 在列表解构中来捕获一个范围的值到子列表中。这在当你想要从列表的开头和结尾提取特定元素,同时收集中间的元素时非常有用。 | ||
1306 | |||
1307 | ```moonscript | ||
1308 | orders = ["first", "second", "third", "fourth", "last"] | ||
1309 | [first, ...bulk, last] = orders | ||
1310 | print first -- 打印: first | ||
1311 | print bulk -- 打印: {"second", "third", "fourth"} | ||
1312 | print last -- 打印: last | ||
1313 | ``` | ||
1314 | <YueDisplay> | ||
1315 | <pre> | ||
1316 | orders = ["first", "second", "third", "fourth", "last"] | ||
1317 | [first, ...bulk, last] = orders | ||
1318 | print first -- 打印: first | ||
1319 | print bulk -- 打印: {"second", "third", "fourth"} | ||
1320 | print last -- 打印: last | ||
1321 | </pre> | ||
1322 | </YueDisplay> | ||
1323 | |||
1324 | 展开运算符可以用在不同的位置来捕获不同的范围,并且你可以使用 `_` 作为占位符来表示你想跳过对应范围的捕获: | ||
1325 | |||
1326 | ```moonscript | ||
1327 | -- 捕获第一个元素之后的所有元素 | ||
1328 | [first, ...rest] = orders | ||
1329 | |||
1330 | -- 捕获最后一个元素之前的所有元素 | ||
1331 | [...start, last] = orders | ||
1332 | |||
1333 | -- 跳过中间的元素,只捕获第一个和最后一个元素 | ||
1334 | [first, _..., last] = orders | ||
1335 | ``` | ||
1336 | <YueDisplay> | ||
1337 | <pre> | ||
1338 | -- 捕获第一个元素之后的所有元素 | ||
1339 | [first, ...rest] = orders | ||
1340 | |||
1341 | -- 捕获最后一个元素之前的所有元素 | ||
1342 | [...start, last] = orders | ||
1343 | |||
1344 | -- 跳过中间的元素,只捕获第一个和最后一个元素 | ||
1345 | [first, _..., last] = orders | ||
1346 | </pre> | ||
1347 | </YueDisplay> | ||
1348 | |||
1349 | ### 在其它地方的解构赋值 | ||
1289 | 1350 | ||
1290 | 解构也可以出现在其它隐式进行赋值的地方。一个例子是用在for循环: | 1351 | 解构赋值也可以出现在其它隐式进行赋值的地方。一个例子是用在for循环中: |
1291 | 1352 | ||
1292 | ```moonscript | 1353 | ```moonscript |
1293 | tuples = [ | 1354 | tuples = [ |
@@ -2305,6 +2366,45 @@ slice = [item for item in *items[,,2]] | |||
2305 | </pre> | 2366 | </pre> |
2306 | </YueDisplay> | 2367 | </YueDisplay> |
2307 | 2368 | ||
2369 | 最小和最大边界都可以是负数,使用负数意味着边界是从表的末尾开始计算的。 | ||
2370 | |||
2371 | ```moonscript | ||
2372 | -- 取最后4个元素 | ||
2373 | slice = [item for item in *items[-4,-1]] | ||
2374 | ``` | ||
2375 | <YueDisplay> | ||
2376 | <pre> | ||
2377 | -- 取最后4个元素 | ||
2378 | slice = [item for item in *items[-4,-1]] | ||
2379 | </pre> | ||
2380 | </YueDisplay> | ||
2381 | |||
2382 | 切片的步长也可以是负数,这意味着元素会以相反的顺序被取出。 | ||
2383 | |||
2384 | ```moonscript | ||
2385 | reverse_slice = [item for item in *items[-1,1,-1]] | ||
2386 | ``` | ||
2387 | <YueDisplay> | ||
2388 | <pre> | ||
2389 | reverse_slice = [item for item in *items[-1,1,-1]] | ||
2390 | </pre> | ||
2391 | </YueDisplay> | ||
2392 | |||
2393 | #### 切片表达式 | ||
2394 | |||
2395 | 切片也可以作为表达式来使用。可以用于获取一个表包含的子列表。 | ||
2396 | |||
2397 | ```moonscript | ||
2398 | -- 取第2和第4个元素作为新的列表 | ||
2399 | sub_list = items[2, 4] | ||
2400 | ``` | ||
2401 | <YueDisplay> | ||
2402 | <pre> | ||
2403 | -- 取第2和第4个元素作为新的列表 | ||
2404 | sub_list = items[2, 4] | ||
2405 | </pre> | ||
2406 | </YueDisplay> | ||
2407 | |||
2308 | ## for 循环 | 2408 | ## for 循环 |
2309 | 2409 | ||
2310 | Lua中有两种for循环形式,数字型和通用型: | 2410 | Lua中有两种for循环形式,数字型和通用型: |
@@ -2948,6 +3048,27 @@ switch tb | |||
2948 | </pre> | 3048 | </pre> |
2949 | </YueDisplay> | 3049 | </YueDisplay> |
2950 | 3050 | ||
3051 | 匹配一个列表并捕获特定范围内的元素。 | ||
3052 | |||
3053 | ```moonscript | ||
3054 | segments = ["admin", "users", "logs", "view"] | ||
3055 | switch segments | ||
3056 | when [...groups, resource, action] | ||
3057 | print "Group:", groups -- 打印: {"admin", "users"} | ||
3058 | print "Resource:", resource -- 打印: "logs" | ||
3059 | print "Action:", action -- 打印: "view" | ||
3060 | ``` | ||
3061 | <YueDisplay> | ||
3062 | <pre> | ||
3063 | segments = ["admin", "users", "logs", "view"] | ||
3064 | switch segments | ||
3065 | when [...groups, resource, action] | ||
3066 | print "Group:", groups -- 打印: {"admin", "users"} | ||
3067 | print "Resource:", resource -- 打印: "logs" | ||
3068 | print "Action:", action -- 打印: "view" | ||
3069 | </pre> | ||
3070 | </YueDisplay> | ||
3071 | |||
2951 | ## 面向对象编程 | 3072 | ## 面向对象编程 |
2952 | 3073 | ||
2953 | 在以下的示例中,月之脚本生成的Lua代码可能看起来会很复杂。所以最好主要关注月之脚本代码层面的意义,然后如果你想知道关于面向对象功能的实现细节,再查看Lua代码。 | 3074 | 在以下的示例中,月之脚本生成的Lua代码可能看起来会很复杂。所以最好主要关注月之脚本代码层面的意义,然后如果你想知道关于面向对象功能的实现细节,再查看Lua代码。 |