On Thu, Oct 28, 2010 at 12:14 PM, Christofer Bogaso
<bogaso.christo...@gmail.com> wrote:
> Dear Barry, this is really interesting. However I could not understand
> this line:
>
> Ops.ss=function(e1,e2){paste(e1,e2)}
>
> Where you have told R to behave "+" function differently when it faces
> "ss" class?
> What should be the ideal approach if I what to use "*" function?

 You can get the character string of the operator from '.Generic', for example:

Ops.ss=function(e1,e2){
   if(.Generic=="+"){
    return(paste(e1,e2))
   }
   if(.Generic=="*"){
     return(rep(e1,e2))
   }
  stop("No definition for ",.Generic)
}

Giving:

 > a*5
 [1] "hello" "hello" "hello" "hello" "hello"
 > a+"world"
 [1] "hello world"
 > a/2
 Error in Ops.ss(a, 2) : No definition for /

Note how S3 methods are dispatched only by reference to the first
argument (on the left of the operator). I think S4 beats this by
having signatures that can dispatch depending on both arguments.

Barry

______________________________________________
R-help@r-project.org mailing list
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.

Reply via email to