From jmc at r-project.org  Tue May 23 15:12:24 2006
From: jmc at r-project.org (John Chambers)
Date: Tue, 23 May 2006 09:12:24 -0400
Subject: [Rd] confused by inheritance...
In-Reply-To: <44721E69.4070305@uni-bayreuth.de>
References: <44721E69.4070305@uni-bayreuth.de>
Message-ID: <44730A38.50301@r-project.org>

This may be related to the problems with "greedy" search for inherited 
methods discussed previously on this list (or may not, it's a bit 
different).

In any case, the likely fix will be a substantial cleaning up of generic 
functions hoped for in the next release.

Meanwhile, "Keep it simple" is the best advice (and a good idea 
anyway).  If you remove the irrelevant z= part of the signatures, the 
anomalous result goes away.

By the way, the getMethod() in your example tells us nothing, since it 
does not use inheritance.  See its documentation.  If you use 
selectMethod() you see the same result as the method dispatch.

Peter Ruckdeschel wrote:

>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
>
>______________________________________________
>R-devel at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-devel
>
>  
>



[Rd] confused by inheritance...
John Chambers