[Rd] WishList: Remove Generic Arguments

Duncan Murdoch murdoch@dunc@n @ending from gm@il@com
Fri Aug 10 00:20:54 CEST 2018


On 09/08/2018 5:45 PM, Abs Spurdle wrote:
>   I apologize if this issue has been raised before.
> 
> I really like object oriented S3 programming.
> However, there's one feature of object oriented S3 programming that I don't
> like.
> Generic functions can have arguments other than dots.
> 
> Lets say you have an R package with something like:
> 
> print.myfunction (f, ...)
> {   dosomething (f, ...)
> }
> 
> Noting that I use function objects a lot.
> 
> R CMD check will generate a warning because you've named your object f
> rather than x.
> 
> I don't want to name my object x.
> I want to name my object f.

There are reasons for this requirement.  Suppose I have an object of 
class myfunction named myobj.  However, being a 3rd party, I have no 
idea what class myfunction is about.  Then I might say

print(x = myobj)

That would not work with your method, because the x would be absorbed 
into ..., it would not bind to f.


> Naming the object x makes the program unreadable.
> Especially if f contains an attribute or an argument named x.

That's nonsense.  You don't need to name your object the same as an 
argument.  You name your object something readable (e.g. myobj), then 
pass it to print(), which binds it to x.

If x is not a sensible name within the print.myfunction() method, then 
there's a one line fix:

print.myfunction <- function(x, ...) {
   f <- x
   dosomething(f)
}
> 
> There's a work around.
> You can redefine the print function, using something like:
> 
> print = function (...) base::print (...)
> 
> However, you have to export and document the function.

That's a really, really bad idea.  If there are two generics named the 
same, how are your users going to know which one they are getting when 
they just say print(myobj)?

> 
> I think that it would be better if generic functions didn't have any
> arguments except for dots.

That makes argument checking much harder.  If a generic always needs two 
arguments, why not name them, so R can complain when a user calls it 
with just one?

Duncan Murdoch



More information about the R-devel mailing list