Search code examples
rclassoptional-parameterstypeof

What kind of object is `...`?


I recently thought about the ... argument for a function and noticed that R does not allow to check the class of the object.

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

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

Now with the quote

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

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

— John Chambers

in my head I'm wondering: What kind of object is ...?


Solution

  • What an interesting question!

    Dot-dot-dot ... is an object (John Chambers is right!) and it's a type of pairlist. Well, I searched the documentation, so I'd like to share it with you:

    R Language Definition document says:

    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 defines pairlists in detail:

    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”).

    Help on Generic and Dotted Pairs says:

    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).

    And a nice summary is here at Stack Overflow!