From murdoch@dunc@n @ending from gm@il@com  Fri Aug 10 00:20:54 2018
From: murdoch@dunc@n @ending from gm@il@com (Duncan Murdoch)
Date: Thu, 9 Aug 2018 18:20:54 -0400
Subject: [Rd] WishList: Remove Generic Arguments
In-Reply-To: <CAB8pepwoUPZRQ8V_OCaZq=QdM3fRbT4zYa8OjOgZvKucmSVFQA@mail.gmail.com>
References: <CAB8pepwoUPZRQ8V_OCaZq=QdM3fRbT4zYa8OjOgZvKucmSVFQA@mail.gmail.com>
Message-ID: <bcc81123-3c54-2a64-8396-6492eaf7165b@gmail.com>

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



[Rd] WishList: Remove Generic Arguments
Duncan Murdoch