classes and methods
mtmorgan at fhcrc.org wrote:
setClass("foo",representation(x="numeric"))
This creates an *S4* class which HAS A 'slot' x that is of type numeric.
setMethod("plot","foo",function(x,y,...)boxplot(x,...))
x <- rnorm(100)
class(x) <- "foo"
uh oh, this is creating an *S3* class that IS A numeric that has been given the class name "foo". The S3 class 'foo' and S4 class 'foo' are different. Note that you can make anything an S3 class
z=letters class(z) <- "foo"
because S3 does not have any definition for what a 'foo' class is.
plot(x)
this 'works' but in my view should not -- the method is defined for the S4 class, not for the S3 class.
it seems that a generic can dispatch to s3 method on an s4 object, and
to an s4 method on an s3 object, and also that s4 methods capture, or
shadow, s3 methods:
setClass('foo', representation(x='numeric'))
setMethod('print', 'foo', function(x, ...) print('foo'))
s4 = new('foo', x = 0)
print(s4)
# "foo"
s3 = structure(0, class='foo')
print(s3)
# "foo"
print.foo = function(s3) print('bar')
print(s3)
# "foo"
print(s4)
# "foo"
removeMethod('print', 'foo')
print(s3)
# "bar"
print(s4)
# "bar"
is this intended?
vQ