[R] classes and methods
Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Fri Mar 20 11:03:17 CET 2009
mtmorgan at fhcrc.org wrote:
> This creates an *S4* class which HAS A 'slot' x that is of type numeric.
>>> 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
>> class(z) <- "foo"
> because S3 does not have any definition for what a 'foo' class is.
> 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:
setMethod('print', 'foo', function(x, ...) print('foo'))
s4 = new('foo', x = 0)
s3 = structure(0, class='foo')
print.foo = function(s3) print('bar')
is this intended?
More information about the R-help