WishList: Remove Generic Arguments

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

WishList: Remove Generic Arguments

Abs Spurdle
 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.
Naming the object x makes the program unreadable.
Especially if f contains an attribute or an argument named x.

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.

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

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: WishList: Remove Generic Arguments

Michael Lawrence-3
A generic function is not simply a way to name two functions (methods)
the same. It has a particular purpose, and the argument names are
aligned with and convey that purpose. The methods only implement
polymorphism; they don't change the purpose. Changing the purpose
would make code unreadable.

Michael

On Thu, Aug 9, 2018 at 2:45 PM, Abs Spurdle <[hidden email]> 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.
> Naming the object x makes the program unreadable.
> Especially if f contains an attribute or an argument named x.
>
> 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.
>
> I think that it would be better if generic functions didn't have any
> arguments except for dots.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: WishList: Remove Generic Arguments

Duncan Murdoch-2
In reply to this post by Abs Spurdle
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: WishList: Remove Generic Arguments

Abs Spurdle
 > 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 <[hidden email]>
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]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel