When to use `...length()` versus `rlang::dots_n(...)`?

76 views Asked by At

In a function, I have to count the number of parameters that are passed through the ellipsis.

I always used ...length() so far, but I recently became aware of rlang::dots_n(...).

They seem to yield the same output in my tests so far:

f = function(x=1, ...){
  print(paste0("base::...length() = ", ...length()))
  print(paste0("rlang::dots_n() = ", rlang::dots_n(...)))
}
f(a=1, b=2, d)
#> [1] "base::...length() = 2"
#> [1] "rlang::dots_n() = 2"

Created on 2024-02-02 with reprex v2.0.2

I expect that rlang devs would not create a function that adds no benefit over a base function, but there is no mention of ...length() in help(dots_n) to outline a comparison.

Is there any significant difference I am not aware of that could prevent or add some bugs?

0

There are 0 answers