[R] oddity with method definition

Faheem Mitha faheem at email.unc.edu
Mon Aug 27 23:47:07 CEST 2007


Just wondered about this curious behaviour. I'm trying to learn about 
classes. Basically setMethod works the first time, but does not seem to 
work the second time.
                                                                 Faheem.
*************************************************************************
setClass("foo", representation(x="numeric"))

bar <- function(object)
   {
     return(0)
   }

bar.foo <- function(object)
   {
     print(object at x)
   }
setMethod("bar", "foo", bar.foo)

bar(f)

# bar(f) gives 1.

bar <- function(object)
   {
     return(0)
   }

bar.foo <- function(object)
   {
     print(object at x)
   }
setMethod("bar", "foo", bar.foo)

f = new("foo", x= 1)

bar(f)

# bar(f) gives 0, not 1.



More information about the R-help mailing list