From 7ee395d918b97795f151c24ed877bfcc2edf602a Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 25 Dec 2025 00:00:08 +0800 Subject: Added named vararg support. --- doc/docs/doc/README.md | 49 +++++++++++++++++++++++++++++++++++++++++++++++ doc/docs/zh/doc/README.md | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) (limited to 'doc') 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 +### Named Varargs + +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). + +```moonscript +f = (...t) -> + print "argument count:", t.n + print "table length:", #t + for i = 1, t.n + print t[i] + +f 1, 2, 3 +f "a", "b", "c", "d" +f! + +-- Handling cases with nil values +process = (...args) -> + sum = 0 + for i = 1, args.n + if args[i] != nil and type(args[i]) == "number" + sum += args[i] + sum + +process 1, nil, 3, nil, 5 +``` + +
+f = (...t) ->
+  print "argument count:", t.n
+  print "table length:", #t
+  for i = 1, t.n
+    print t[i]
+
+f 1, 2, 3
+f "a", "b", "c", "d"
+f!
+
+-- Handling cases with nil values
+process = (...args) ->
+  sum = 0
+  for i = 1, args.n
+    if args[i] != nil and type(args[i]) == "number"
+      sum += args[i]
+  sum
+
+process 1, nil, 3, nil, 5
+
+
+ ## Whitespace 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 +### 命名变长参数 + +你可以使用 `(...t) ->` 语法来将变长参数自动存储到一个命名表中。这个表会包含所有传入的参数(包括 `nil` 值),并且会在表的 `n` 字段中存储实际传入的参数个数(包括 `nil` 值在内的个数)。 + +```moonscript +f = (...t) -> + print "参数个数:", t.n + print "表长度:", #t + for i = 1, t.n + print t[i] + +f 1, 2, 3 +f "a", "b", "c", "d" +f! + +-- 处理包含 nil 的情况 +process = (...args) -> + sum = 0 + for i = 1, args.n + if args[i] != nil and type(args[i]) == "number" + sum += args[i] + sum + +process 1, nil, 3, nil, 5 +``` + +
+f = (...t) ->
+  print "参数个数:", t.n
+  print "表长度:", #t
+  for i = 1, t.n
+    print t[i]
+
+f 1, 2, 3
+f "a", "b", "c", "d"
+f!
+
+-- 处理包含 nil 的情况
+process = (...args) ->
+  sum = 0
+  for i = 1, args.n
+    if args[i] != nil and type(args[i]) == "number"
+      sum += args[i]
+  sum
+
+process 1, nil, 3, nil, 5
+
+
+ ## 空白 月之脚本是一个对空白敏感的语言。你必须在相同的缩进中使用空格 **' '** 或制表符 **'\t'** 来编写一些代码块,如函数体、值列表和一些控制块。包含不同空白的表达式可能意味着不同的事情。制表符被视为4个空格,但最好不要混合使用空格和制表符。 -- cgit v1.2.3-55-g6feb