... 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) paste0(e1, e2) ## but this doesn't 'work':> "a" + "b"Error in "a" + "b" : non-numeric argument to binary operator ## but explicitly invoking the method does 'work' :> "+.character"('a','b')[1] "ab" ##Note also:> methods("+")[1] +.character +.Date +.IDate* +.POSIXt +.trellis* So what am I failing to understand? Thanks. Bert Gunter
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 <bgunter.4567 at gmail.com> wrote:>... 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) paste0(e1, e2) >## but this doesn't 'work': >> "a" + "b" >Error in "a" + "b" : non-numeric argument to binary operator > >## but explicitly invoking the method does 'work' : >> "+.character"('a','b') >[1] "ab" > >##Note also: >> methods("+") >[1] +.character +.Date +.IDate* +.POSIXt +.trellis* > >So what am I failing to understand? >Thanks. > >Bert Gunter > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.
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 class attribute, which is different from UseMethod which calls class() if the class attribute is NULL. I think if you want to define something like what you have written, you could write a function `%+%` use that instead On Thu, Dec 2, 2021, 14:32 Bert Gunter <bgunter.4567 at gmail.com> wrote:> ... 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) paste0(e1, e2) > ## but this doesn't 'work': > > "a" + "b" > Error in "a" + "b" : non-numeric argument to binary operator > > ## but explicitly invoking the method does 'work' : > > "+.character"('a','b') > [1] "ab" > > ##Note also: > > methods("+") > [1] +.character +.Date +.IDate* +.POSIXt +.trellis* > > So what am I failing to understand? > Thanks. > > Bert Gunter > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]