在S3函数重载中应该将省略号放在哪里?

时间:2019-01-08 16:56:33

标签: r r-s3

我们有一个S3类,我们为其定义了plot和其他通用函数。我们不确定...的去向。有两种选择:

  1. plot.hadronacf(x, col = "black", ...)
  2. plot.hadronacf(x, ..., col = "black")

print.summary.类似。

summary的用法中似乎不一致:

summary(object, ...)

## Default S3 method:
summary(object, ..., digits)
## S3 method for class 'data.frame'
summary(object, maxsum = 7,
       digits = max(3, getOption("digits")-3), ...)

## S3 method for class 'factor'
summary(object, maxsum = 100, ...)

## S3 method for class 'matrix'
summary(object, ...)

## S3 method for class 'summaryDefault'
format(x, digits = max(3L, getOption("digits") - 3L), ...)
 ## S3 method for class 'summaryDefault'
print(x, digits = max(3L, getOption("digits") - 3L), ...)

对于print,省略号似乎要结尾:

print(x, ...)

## S3 method for class 'factor'
print(x, quote = FALSE, max.levels = NULL,
      width = getOption("width"), ...)

## S3 method for class 'table'
print(x, digits = getOption("digits"), quote = FALSE,
      na.print = "", zero.print = "0",
      right = is.numeric(x) || is.complex(x),
      justify = "none", ...)

## S3 method for class 'function'
print(x, useSource = TRUE, ...)

似乎多数情况下使用了省略号。有一些指导方针吗?

1 个答案:

答案 0 :(得分:4)

没有做到这一点的“正确”方法。根据您认为该功能应该对“额外参数”执行的操作,这是偏好问题或设计决策。例如,有两个变体A和B

summary(object, maxsum = 100, ...)  # A
summary(object, ..., maxsum = 100)  # B

maxsum传递给版本B的唯一方法是通过函数调用中的命名参数。而版本A将采用第二个未命名参数,并将其传递给maxsum。它们在参数对函数调用的“重要性”方面有所不同。