[Rd] S4 methods with default argument of NULL

Benjamin Tyner btyner @end|ng |rom gm@||@com
Sat Nov 6 14:51:31 CET 2021


Greetings,

I noticed that starting with R version 3.5.0, it is now possible to do, 
for example:

    setGeneric(name = "foo",
                def = function(x, y, ...) standardGeneric("foo"),
                valueClass = "fooResult"
                )

    setMethod(f = "foo",
               signature = c("character", "character"),
               definition = function(x, y = NULL, z = NULL, ...) {

                   force(y)
                   message(sprintf("x is %s", x))

                   structure(list(), class = "fooResult")
               })

    foo("a", "b") # this works in R >= 3.5.0, but fails in R < 3.5.0

(Prior to R version 3.5.0, it gives "Error in force(y) : object 'y' not 
found")

I am curious to learn whether the change permitting this was:

    1. an intentional enhancement
    2. unintentional, but a "happy accident" (i.e., a desirable side
    effect of some other enhancement)
    3. unintentional, and perhaps undesirable (i.e., a loosening of
    standards)

For reference, this came up in the debugging of the following issue:

    https://github.com/DyfanJones/noctua/issues/170

Any comments to shed some light would be greatly appreciated.

Regards,

Ben



More information about the R-devel mailing list