[Rd] confused by inheritance...

Peter Ruckdeschel Peter.Ruckdeschel at uni-bayreuth.de
Mon May 22 22:26:17 CEST 2006


Hi r-devels,

I am stuck in some S4 inheritance problem:

setClass("A",representation(a="numeric"))
setClass("A1",representation(b="numeric"),contains="A")
setClass("A2",representation(c="numeric"),contains="A1")

if(!isGeneric("foo")){
    setGeneric("foo", function(x,y,z, ...) standardGeneric("foo"))
}

setMethod("foo",signature(x = "A", y = "missing", z = "missing"),
    function(x)x at a )
setMethod("foo",signature(x = "A1", y = "missing", z = "missing"),
    function(x)x at b )
setMethod("foo",signature(x = "A2", y = "missing", z = "missing"),
    function(x)x at c )
setMethod("foo",signature(x = "A1", y = "numeric", z = "missing"),
    function(x,y)c(x at b,y) )


x2 <- new("A2", a=1, b=2, c=3)

foo(x2)      ## gives 3 as it should
foo(x2,y=2)  ## casts to "A1" and gives (2,2) as it should
foo(x2)      ## now gives 2 as if x2 were permanently cast to "A1"
## However:
x2  ## of class "A2" as it should
getMethod("foo",signature(x = "A2", y = "missing", z = "missing"))
    ## function(x)x at c

### What has happened in the dispatching mechanism between
## in the line           foo(x2,y=2)             ?

I would appreciate any help.
Thanks for listening
Peter



More information about the R-devel mailing list