[R-pkg-devel] [External] Re: S3 generic/method consistency warning for formula method

Smith, Brian J br|@n-j-@m|th @end|ng |rom u|ow@@edu
Fri Jan 17 18:45:29 CET 2020


Thanks for the tip about dotsMethods.  I am mainly working with S4 objects in my package, so that approach would fit well.  The following S4 method seems to work and pass R CMD checks.

setGeneric("foo2", function(...) standardGeneric("foo2"))

setMethod("foo2", "formula",
  function(...) list(...)
)

In my actual implementation of S3 methods, I do check that all ... elements are of the same class to avoid ending up in a world of hurt.  Converting them to S4 methods will save me from having to do those checks - nice!

>From a language definition point of view though, I am still bothered that a check warning is triggered by the S3 formula method but not by others.  That just seems inconsistent, and I have not yet seen any language documentation to explain the inconsistency.  Are we sure this is not an issue with the R CMD check implementation?

Nevertheless, it looks like I have a way to get rid of the warning.

-----Original Message-----
From: Joris Meys <Joris.Meys using UGent.be> 
Sent: Friday, January 17, 2020 10:12 AM
To: Smith, Brian J <brian-j-smith using uiowa.edu>; Duncan Murdoch <murdoch.duncan using gmail.com>; r-package-devel using r-project.org
Subject: Re: [R-pkg-devel] [External] Re: S3 generic/method consistency warning for formula method

Hi Brian,

I get what you want to do, but S3 isn't suited for that at all. If all elements in ... have to be the same class, you can use S4 (see ?dotsMethods). If not, then using S3 makes even less sense. Take following example:

x <- list()
y <- c(1,2)

foo(x,y) will dispatch to foo.list()
foo(y,x) will dispatch to foo.character()

You'll be in a world of hurt if you want to maintain or update that code. If you insist on using S3, you'll have to fix the position (and hence the name) of the argument you want to dispatch on.

my 2 cents
Cheers
Joris

--
Joris Meys
Statistical consultant

Department of Data Analysis and Mathematical Modelling Ghent University Coupure Links 653, B-9000 Gent (Belgium)
------------------------------

Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php


________________________________________
From: R-package-devel <r-package-devel-bounces using r-project.org> on behalf of Smith, Brian J <brian-j-smith using uiowa.edu>
Sent: Friday, January 17, 2020 4:59 PM
To: Duncan Murdoch; r-package-devel using r-project.org
Subject: Re: [R-pkg-devel]  [External] Re: S3 generic/method consistency warning for formula method

Thanks Duncan.

I was surprised too when first realizing this was possible.  I believe the reason it works is that UseMethod dispatches on the first supplied argument by default.  So, really the dispatch is on the first element of the ellipsis.  The 'c' function works like this, albeit as a primitive function.

I would like to dispatch on the ellipsis to allow a user to name values being passed in.  Defining an initial x argument would indeed eliminate the warning, but would not allow for user-naming of the value.  I could have the user pass a list of values to x and do dispatching manually, but that seems like more steps for the user and more complication for the implementation than ought to be necessary.  There are other downsides to that approach in my particularly application...

The issue mentioned below about calling the function with no arguments can be solved by defining the following default method.

foo.default <- function(...) list(...)



More information about the R-package-devel mailing list