[Rd] WishList: Remove Generic Arguments

Abs Spurdle @purdle@@ @ending from gm@il@com
Sat Aug 11 01:03:13 CEST 2018


 > 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)
> }

Naming the argument x is not an option.

>>  print = function (...) base::print (...)

>  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)?

If redefining the generics is "a really, really bad idea" then an
alternative would be to define a new generic and two methods, one of which
acts as a kind of bridging function, using something like:

myprint = function (...) UseMethod ("myprint")
myprint.myfunction = function (f, ...) {dosomething (f, ...)}

print.myfunction = function (x, ...) myprint (x, ...)

>  If a generic always needs two arguments, why not name them, so R can
complain when a user calls it with just one?

We can apply that principle to the methods rather than the generic.



On Fri, Aug 10, 2018 at 10:20 AM, Duncan Murdoch <murdoch.duncan using gmail.com>
wrote:

> 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
>

	[[alternative HTML version deleted]]



More information about the R-devel mailing list