`...` 是什么对象?

What kind of object is `...`?

我最近考虑了函数的 ... 参数,并注意到 R 不允许检查对象的 class。

f <- function(...) {
   class(...)
}

f(1, 2, 3)
## Error in class(...) : 3 arguments passed to 'class' which requires 1

现在引用

“To understand computations in R, two slogans are helpful:

• Everything that exists is an object. • Everything that happens is a function call."

— John Chambers

我在想:... 是个什么样的物体?

多么有趣的问题!

点-点-点 ... 是一个对象(John Chambers 是对的!),它是一种配对列表。嗯,我查了下文档,分享给大家:

R Language Definition 文档说:

The ‘...’ object type is stored as a type of pairlist. The components of ‘...’ can be accessed in the usual pairlist manner from C code, but is not easily accessed as an object in interpreted code. The object can be captured as a list.

Another chapter 详细定义配对列表:

Pairlist objects are similar to Lisp’s dotted-pair lists.

Pairlists are handled in the R language in exactly the same way as generic vectors (“lists”).

关于 Generic and Dotted Pairs 的帮助说:

Almost all lists in R internally are Generic Vectors, whereas traditional dotted pair lists (as in LISP) remain available but rarely seen by users (except as formals of functions).

上有一个很好的总结!