Davis Vaughan
2021-Jan-28 14:14 UTC
[Rd] Allowing S3 methods of rounding functions to take `...`
Hi all, I would like to propose adding `...` to the signatures of the following rounding functions: - floor(x) - ceiling(x) - round(x, digits = 0) - And possibly signif(x, digits = 6) The purpose would be to allow S3 methods to add additional arguments as required. A few arguments in favor of this change: `trunc(x, ...)` already takes dots, which sets a precedent for the others to do so as well. It is documented in the same help file as the other rounding functions. Internally at the C level, a check is done to ensure that there is exactly 1 arg for floor() and ceiling(), and either 1 or 2 args for round(). The actual names of those arguments are not checked, however, and I believe this is what allows `round.Date(x, ...)` and `round.POSIXt(x, unit)` to exist, solely because they have 2 arguments. It seems like this is a bit of a hack, since you couldn't create something similar for floor, like `floor.POSIXt(x, unit)` (not saying this should exist, it is just for argument's sake), because the 1 argument check would error on this. I think adding `...` to the signature of the generics would better support what is being done here. Additionally, I have a custom date-like S3 class of my own that I would like to write floor(), ceiling(), and round() methods for, and they would require passing additional arguments. If R core would like to make this change, they could probably tweak `do_trunc()` to be a bit more general, and use it for floor() and ceiling(), since it already allows `...`. A few references: Check for 1 arg in do_math1(), used by floor() and ceiling() https://github.com/wch/r-source/blob/fe82da3baf849fcd3cc7dbc31c6abc72b57aa083/src/main/arithmetic.c#L1270 Check for 2 args in do_Math2(), used by round() https://github.com/wch/r-source/blob/fe82da3baf849fcd3cc7dbc31c6abc72b57aa083/src/main/arithmetic.c#L1655 do_trunc() definition that allows `...` https://github.com/wch/r-source/blob/fe82da3baf849fcd3cc7dbc31c6abc72b57aa083/src/main/arithmetic.c#L1329-L1340 - Davis [[alternative HTML version deleted]]
Davis Vaughan
2021-Jan-28 15:47 UTC
[Rd] Allowing S3 methods of rounding functions to take `...`
I should also say that I would be willing to attempt a patch for this, if others agree that this would be useful. - Davis On Thu, Jan 28, 2021 at 9:14 AM Davis Vaughan <davis at rstudio.com> wrote:> Hi all, > > I would like to propose adding `...` to the signatures of the following > rounding functions: > > - floor(x) > - ceiling(x) > - round(x, digits = 0) > - And possibly signif(x, digits = 6) > > The purpose would be to allow S3 methods to add additional arguments as > required. > > A few arguments in favor of this change: > > `trunc(x, ...)` already takes dots, which sets a precedent for the others > to do so as well. It is documented in the same help file as the other > rounding functions. > > Internally at the C level, a check is done to ensure that there is exactly > 1 arg for floor() and ceiling(), and either 1 or 2 args for round(). The > actual names of those arguments are not checked, however, and I believe > this is what allows `round.Date(x, ...)` and `round.POSIXt(x, unit)` to > exist, solely because they have 2 arguments. It seems like this is a bit of > a hack, since you couldn't create something similar for floor, like > `floor.POSIXt(x, unit)` (not saying this should exist, it is just for > argument's sake), because the 1 argument check would error on this. I think > adding `...` to the signature of the generics would better support what is > being done here. > > Additionally, I have a custom date-like S3 class of my own that I would > like to write floor(), ceiling(), and round() methods for, and they would > require passing additional arguments. > > If R core would like to make this change, they could probably tweak > `do_trunc()` to be a bit more general, and use it for floor() and > ceiling(), since it already allows `...`. > > A few references: > > Check for 1 arg in do_math1(), used by floor() and ceiling() > > https://github.com/wch/r-source/blob/fe82da3baf849fcd3cc7dbc31c6abc72b57aa083/src/main/arithmetic.c#L1270 > > Check for 2 args in do_Math2(), used by round() > > https://github.com/wch/r-source/blob/fe82da3baf849fcd3cc7dbc31c6abc72b57aa083/src/main/arithmetic.c#L1655 > > do_trunc() definition that allows `...` > > https://github.com/wch/r-source/blob/fe82da3baf849fcd3cc7dbc31c6abc72b57aa083/src/main/arithmetic.c#L1329-L1340 > > - Davis >[[alternative HTML version deleted]]