Hadley, The class AB inherits from A and from B, but B already inherits from class A. So actually you only have an object of class B in your object of class AB. When you call the function f R looks for a method f for AB objects. It does not find such a method and looks for a method of the object inherited from, B. Such a method is present and is then executed.
The inheritance structure has to be changed. The behavior is actually desired, as if this behavior weren't given a diamond class inheritance would be fatal. Best Simon On Aug 13, 2013, at 3:08 PM, Hadley Wickham <[email protected]> wrote: > Hi all, > > Any insight into the code below would be appreciated - I don't > understand why two methods which I think should have equal distance > from the call don't. > > Thanks! > > Hadley > > # Create simple class hierarchy > setClass("A", "NULL") > setClass("B", "A") > > a <- new("A") > b <- new("B") > > setGeneric("f", function(x, y) standardGeneric("f")) > setMethod("f", signature("A", "A"), function(x, y) "A-A") > setMethod("f", signature("B", "B"), function(x, y) "B-B") > > # These work as I expect > f(a, a) > f(b, b) > > setClass("AB", contains = c("A", "B")) > ab <- new("AB") > > # Why does this return B-B? Shouldn't both methods be an equal distance? > f(ab, ab) > > # These both return distance 1, as I expected > extends("AB", "A", fullInfo=TRUE)@distance > extends("AB", "B", fullInfo=TRUE)@distance > # So why is signature("B", "B") closer than signature("A", "A") > > -- > Chief Scientist, RStudio > http://had.co.nz/ > > ______________________________________________ > [email protected] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

