aboutsummaryrefslogtreecommitdiff
path: root/doc/docs
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2025-06-04 16:43:26 +0800
committerLi Jin <dragon-fly@qq.com>2025-06-04 16:43:26 +0800
commita575de6992cabe0b809e363391563841d6213d4e (patch)
treed7a711e65b114d6a2a87988c1e611b1583bc1d45 /doc/docs
parent0e72454a11f65d9ac800dedb698ddfcc15933785 (diff)
downloadyuescript-a575de6992cabe0b809e363391563841d6213d4e.tar.gz
yuescript-a575de6992cabe0b809e363391563841d6213d4e.tar.bz2
yuescript-a575de6992cabe0b809e363391563841d6213d4e.zip
Updated docs. [skip CI]
Diffstat (limited to 'doc/docs')
-rwxr-xr-xdoc/docs/doc/README.md121
-rwxr-xr-xdoc/docs/zh/doc/README.md125
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
591You can use the **#** operator to get the last elements of a table.
592
593```moonscript
594print data.items[#] -- get the last element of a table
595print data.items[#-1] -- get the second last element of a table
596```
597<YueDisplay>
598<pre>
599print data.items[#] -- get the last element of a table
600print data.items[#-1] -- get the second last element of a table
601</pre>
602</YueDisplay>
603
589### Metatable 604### Metatable
590 605
591The **<>** operator can be used as a shortcut for metatable manipulation. 606The **<>** 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
1307You 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
1310orders = ["first", "second", "third", "fourth", "last"]
1311[first, ...bulk, last] = orders
1312print first -- prints: first
1313print bulk -- prints: {"second", "third", "fourth"}
1314print last -- prints: last
1315```
1316<YueDisplay>
1317<pre>
1318orders = ["first", "second", "third", "fourth", "last"]
1319[first, ...bulk, last] = orders
1320print first -- prints: first
1321print bulk -- prints: {"second", "third", "fourth"}
1322print last -- prints: last
1323</pre>
1324</YueDisplay>
1325
1326The 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
1292Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop: 1353Destructuring 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
2408Both 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
2412slice = [item for item in *items[-4,-1]]
2413```
2414<YueDisplay>
2415<pre>
2416-- take the last 4 items
2417slice = [item for item in *items[-4,-1]]
2418</pre>
2419</YueDisplay>
2420
2421The step size can also be negative, which means that the items are taken in reverse order.
2422
2423```moonscript
2424reverse_slice = [item for item in *items[-1,1,-1]]
2425```
2426<YueDisplay>
2427<pre>
2428reverse_slice = [item for item in *items[-1,1,-1]]
2429</pre>
2430</YueDisplay>
2431
2432#### Slicing Expression
2433
2434Slicing 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
2438sub_list = items[2, 4]
2439```
2440<YueDisplay>
2441<pre>
2442-- take the 2nd and 4th items as a new list
2443sub_list = items[2, 4]
2444</pre>
2445</YueDisplay>
2446
2347## For Loop 2447## For Loop
2348 2448
2349There are two for loop forms, just like in Lua. A numeric one and a generic one: 2449There 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
3092Match against a list and capture a range of elements.
3093
3094```moonscript
3095segments = ["admin", "users", "logs", "view"]
3096switch 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>
3104segments = ["admin", "users", "logs", "view"]
3105switch 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
2994In 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. 3115In 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
593print data.items[#] -- 获取表的最后一个元素
594print data.items[#-1] -- 获取表的倒数第二个元素
595```
596<YueDisplay>
597<pre>
598print data.items[#] -- 获取表的最后一个元素
599print 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
1308orders = ["first", "second", "third", "fourth", "last"]
1309[first, ...bulk, last] = orders
1310print first -- 打印: first
1311print bulk -- 打印: {"second", "third", "fourth"}
1312print last -- 打印: last
1313```
1314<YueDisplay>
1315<pre>
1316orders = ["first", "second", "third", "fourth", "last"]
1317[first, ...bulk, last] = orders
1318print first -- 打印: first
1319print bulk -- 打印: {"second", "third", "fourth"}
1320print 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
1293tuples = [ 1354tuples = [
@@ -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个元素
2373slice = [item for item in *items[-4,-1]]
2374```
2375<YueDisplay>
2376<pre>
2377-- 取最后4个元素
2378slice = [item for item in *items[-4,-1]]
2379</pre>
2380</YueDisplay>
2381
2382切片的步长也可以是负数,这意味着元素会以相反的顺序被取出。
2383
2384```moonscript
2385reverse_slice = [item for item in *items[-1,1,-1]]
2386```
2387<YueDisplay>
2388<pre>
2389reverse_slice = [item for item in *items[-1,1,-1]]
2390</pre>
2391</YueDisplay>
2392
2393#### 切片表达式
2394
2395切片也可以作为表达式来使用。可以用于获取一个表包含的子列表。
2396
2397```moonscript
2398-- 取第2和第4个元素作为新的列表
2399sub_list = items[2, 4]
2400```
2401<YueDisplay>
2402<pre>
2403-- 取第2和第4个元素作为新的列表
2404sub_list = items[2, 4]
2405</pre>
2406</YueDisplay>
2407
2308## for 循环 2408## for 循环
2309 2409
2310Lua中有两种for循环形式,数字型和通用型: 2410Lua中有两种for循环形式,数字型和通用型:
@@ -2948,6 +3048,27 @@ switch tb
2948</pre> 3048</pre>
2949</YueDisplay> 3049</YueDisplay>
2950 3050
3051匹配一个列表并捕获特定范围内的元素。
3052
3053```moonscript
3054segments = ["admin", "users", "logs", "view"]
3055switch 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>
3063segments = ["admin", "users", "logs", "view"]
3064switch 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代码。