diff options
Diffstat (limited to 'doc')
| -rwxr-xr-x | doc/docs/doc/README.md | 49 | ||||
| -rwxr-xr-x | doc/docs/zh/doc/README.md | 49 |
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 | |||
| 1533 | You 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 | ||
| 1536 | f = (...t) -> | ||
| 1537 | print "argument count:", t.n | ||
| 1538 | print "table length:", #t | ||
| 1539 | for i = 1, t.n | ||
| 1540 | print t[i] | ||
| 1541 | |||
| 1542 | f 1, 2, 3 | ||
| 1543 | f "a", "b", "c", "d" | ||
| 1544 | f! | ||
| 1545 | |||
| 1546 | -- Handling cases with nil values | ||
| 1547 | process = (...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 | |||
| 1554 | process 1, nil, 3, nil, 5 | ||
| 1555 | ``` | ||
| 1556 | <YueDisplay> | ||
| 1557 | <pre> | ||
| 1558 | f = (...t) -> | ||
| 1559 | print "argument count:", t.n | ||
| 1560 | print "table length:", #t | ||
| 1561 | for i = 1, t.n | ||
| 1562 | print t[i] | ||
| 1563 | |||
| 1564 | f 1, 2, 3 | ||
| 1565 | f "a", "b", "c", "d" | ||
| 1566 | f! | ||
| 1567 | |||
| 1568 | -- Handling cases with nil values | ||
| 1569 | process = (...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 | |||
| 1576 | process 1, nil, 3, nil, 5 | ||
| 1577 | </pre> | ||
| 1578 | </YueDisplay> | ||
| 1579 | |||
| 1531 | ## Whitespace | 1580 | ## Whitespace |
| 1532 | 1581 | ||
| 1533 | YueScript 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. | 1582 | YueScript 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 | ||
| 1534 | f = (...t) -> | ||
| 1535 | print "参数个数:", t.n | ||
| 1536 | print "表长度:", #t | ||
| 1537 | for i = 1, t.n | ||
| 1538 | print t[i] | ||
| 1539 | |||
| 1540 | f 1, 2, 3 | ||
| 1541 | f "a", "b", "c", "d" | ||
| 1542 | f! | ||
| 1543 | |||
| 1544 | -- 处理包含 nil 的情况 | ||
| 1545 | process = (...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 | |||
| 1552 | process 1, nil, 3, nil, 5 | ||
| 1553 | ``` | ||
| 1554 | <YueDisplay> | ||
| 1555 | <pre> | ||
| 1556 | f = (...t) -> | ||
| 1557 | print "参数个数:", t.n | ||
| 1558 | print "表长度:", #t | ||
| 1559 | for i = 1, t.n | ||
| 1560 | print t[i] | ||
| 1561 | |||
| 1562 | f 1, 2, 3 | ||
| 1563 | f "a", "b", "c", "d" | ||
| 1564 | f! | ||
| 1565 | |||
| 1566 | -- 处理包含 nil 的情况 | ||
| 1567 | process = (...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 | |||
| 1574 | process 1, nil, 3, nil, 5 | ||
| 1575 | </pre> | ||
| 1576 | </YueDisplay> | ||
| 1577 | |||
| 1529 | ## 空白 | 1578 | ## 空白 |
| 1530 | 1579 | ||
| 1531 | 月之脚本是一个对空白敏感的语言。你必须在相同的缩进中使用空格 **' '** 或制表符 **'\t'** 来编写一些代码块,如函数体、值列表和一些控制块。包含不同空白的表达式可能意味着不同的事情。制表符被视为4个空格,但最好不要混合使用空格和制表符。 | 1580 | 月之脚本是一个对空白敏感的语言。你必须在相同的缩进中使用空格 **' '** 或制表符 **'\t'** 来编写一些代码块,如函数体、值列表和一些控制块。包含不同空白的表达式可能意味着不同的事情。制表符被视为4个空格,但最好不要混合使用空格和制表符。 |
