Re: [R] A technical question on methods for "+"

2021-12-02 Thread Deepayan Sarkar
On Fri, Dec 3, 2021 at 3:14 AM Bert Gunter wrote: > > Those are both helpful comments. Would it not be useful to say > something along these lines in ?class. ? > The key point I missed is that there often(but not always) must be an > *explicit* class **attribute** for method dispatch; and class()

Re: [R] A technical question on methods for "+"

2021-12-02 Thread Bert Gunter
Those are both helpful comments. Would it not be useful to say something along these lines in ?class. ? The key point I missed is that there often(but not always) must be an *explicit* class **attribute** for method dispatch; and class() does not indicate whether its value is explicit or not (just

Re: [R] A technical question on methods for "+"

2021-12-02 Thread Duncan Murdoch
The reason for this behaviour is that finding methods is a lot slower than just evaluating the built-in function. So R takes the time to determine if there's an attribute named "class" attached, but doesn't go searching further if there isn't one. Duncan Murdoch On 02/12/2021 3:10 p.m., Andr

Re: [R] A technical question on methods for "+"

2021-12-02 Thread Andrew Simmons
class() does not always return the class attribute, try something more like attr(, "class"), you'll see what I mean On Thu, Dec 2, 2021, 15:23 Bert Gunter wrote: > "This is because + dispatches on the class attribute, which a string > like "test" has set to NULL" > Not true. > > > class('test')

Re: [R] A technical question on methods for "+"

2021-12-02 Thread Bert Gunter
"This is because + dispatches on the class attribute, which a string like "test" has set to NULL" Not true. > class('test') [1] "character" But apparently, as Denes and Jeff said, the class must be explicitly set, rather than relying on its built-in/implicit type. With the above hint, I looked u

Re: [R] A technical question on methods for "+"

2021-12-02 Thread Andrew Simmons
This is because + dispatches on the class attribute, which a string like "test" has set to NULL, so it doesn't dispatch. You can add the class yourself like structure("test", class = "character") and that should work. I'm not sure where it's explained, but most primitive functions dispatch on the

Re: [R] A technical question on methods for "+"

2021-12-02 Thread Jeff Newmiller
I think the fact that character is a built-in type rather than an S3 class has something to do with it. On December 2, 2021 11:31:47 AM PST, Bert Gunter wrote: >... and probably a dumb one and almost certainly not of interest to >most R users. But anyway... > >?"+" says: >"The unary and binary a

[R] A technical question on methods for "+"

2021-12-02 Thread Bert Gunter
... and probably a dumb one and almost certainly not of interest to most R users. But anyway... ?"+" says: "The unary and binary arithmetic operators are generic functions: methods can be written for them individually or via the Ops group generic function. " So: "+.character" <- function(e1, e2)