aboutsummaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2025-12-25 00:00:08 +0800
committerLi Jin <dragon-fly@qq.com>2025-12-25 00:00:08 +0800
commit7ee395d918b97795f151c24ed877bfcc2edf602a (patch)
tree0fbb5f4d3011ee6459e011416a1307d74cf331a7 /doc
parentbb563257953628a9849c8532a684b5064bf8e655 (diff)
downloadyuescript-7ee395d918b97795f151c24ed877bfcc2edf602a.tar.gz
yuescript-7ee395d918b97795f151c24ed877bfcc2edf602a.tar.bz2
yuescript-7ee395d918b97795f151c24ed877bfcc2edf602a.zip
Added named vararg support.
Diffstat (limited to 'doc')
-rwxr-xr-xdoc/docs/doc/README.md49
-rwxr-xr-xdoc/docs/zh/doc/README.md49
2 files changed, 98 insertions, 0 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md
index 1a46e59..d2f838d 100755
--- a/doc/docs/doc/README.md
+++ b/doc/docs/doc/README.md
@@ -1528,6 +1528,55 @@ print ok, count, first
1528</pre> 1528</pre>
1529</YueDisplay> 1529</YueDisplay>
1530 1530
1531### Named Varargs
1532
1533You can use the `(...t) ->` syntax to automatically store varargs into a named table. This table will contain all passed arguments (including `nil` values), and the `n` field of the table will store the actual number of arguments passed (including `nil` values).
1534
1535```moonscript
1536f = (...t) ->
1537 print "argument count:", t.n
1538 print "table length:", #t
1539 for i = 1, t.n
1540 print t[i]
1541
1542f 1, 2, 3
1543f "a", "b", "c", "d"
1544f!
1545
1546-- Handling cases with nil values
1547process = (...args) ->
1548 sum = 0
1549 for i = 1, args.n
1550 if args[i] != nil and type(args[i]) == "number"
1551 sum += args[i]
1552 sum
1553
1554process 1, nil, 3, nil, 5
1555```
1556<YueDisplay>
1557<pre>
1558f = (...t) ->
1559 print "argument count:", t.n
1560 print "table length:", #t
1561 for i = 1, t.n
1562 print t[i]
1563
1564f 1, 2, 3
1565f "a", "b", "c", "d"
1566f!
1567
1568-- Handling cases with nil values
1569process = (...args) ->
1570 sum = 0
1571 for i = 1, args.n
1572 if args[i] != nil and type(args[i]) == "number"
1573 sum += args[i]
1574 sum
1575
1576process 1, nil, 3, nil, 5
1577</pre>
1578</YueDisplay>
1579
1531## Whitespace 1580## Whitespace
1532 1581
1533YueScript is a whitespace significant language. You have to write some code block in the same indent with space **' '** or tab **'\t'** like function body, value list and some control blocks. And expressions containing different whitespaces might mean different things. Tab is treated like 4 space, but it's better not mix the use of spaces and tabs. 1582YueScript is a whitespace significant language. You have to write some code block in the same indent with space **' '** or tab **'\t'** like function body, value list and some control blocks. And expressions containing different whitespaces might mean different things. Tab is treated like 4 space, but it's better not mix the use of spaces and tabs.
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md
index b348e06..34d577c 100755
--- a/doc/docs/zh/doc/README.md
+++ b/doc/docs/zh/doc/README.md
@@ -1526,6 +1526,55 @@ print ok, count, first
1526</pre> 1526</pre>
1527</YueDisplay> 1527</YueDisplay>
1528 1528
1529### 命名变长参数
1530
1531你可以使用 `(...t) ->` 语法来将变长参数自动存储到一个命名表中。这个表会包含所有传入的参数(包括 `nil` 值),并且会在表的 `n` 字段中存储实际传入的参数个数(包括 `nil` 值在内的个数)。
1532
1533```moonscript
1534f = (...t) ->
1535 print "参数个数:", t.n
1536 print "表长度:", #t
1537 for i = 1, t.n
1538 print t[i]
1539
1540f 1, 2, 3
1541f "a", "b", "c", "d"
1542f!
1543
1544-- 处理包含 nil 的情况
1545process = (...args) ->
1546 sum = 0
1547 for i = 1, args.n
1548 if args[i] != nil and type(args[i]) == "number"
1549 sum += args[i]
1550 sum
1551
1552process 1, nil, 3, nil, 5
1553```
1554<YueDisplay>
1555<pre>
1556f = (...t) ->
1557 print "参数个数:", t.n
1558 print "表长度:", #t
1559 for i = 1, t.n
1560 print t[i]
1561
1562f 1, 2, 3
1563f "a", "b", "c", "d"
1564f!
1565
1566-- 处理包含 nil 的情况
1567process = (...args) ->
1568 sum = 0
1569 for i = 1, args.n
1570 if args[i] != nil and type(args[i]) == "number"
1571 sum += args[i]
1572 sum
1573
1574process 1, nil, 3, nil, 5
1575</pre>
1576</YueDisplay>
1577
1529## 空白 1578## 空白
1530 1579
1531月之脚本是一个对空白敏感的语言。你必须在相同的缩进中使用空格 **' '** 或制表符 **'\t'** 来编写一些代码块,如函数体、值列表和一些控制块。包含不同空白的表达式可能意味着不同的事情。制表符被视为4个空格,但最好不要混合使用空格和制表符。 1580月之脚本是一个对空白敏感的语言。你必须在相同的缩进中使用空格 **' '** 或制表符 **'\t'** 来编写一些代码块,如函数体、值列表和一些控制块。包含不同空白的表达式可能意味着不同的事情。制表符被视为4个空格,但最好不要混合使用空格和制表符。