[R] Understanding S4 method dispatch

Hadley Wickham h.wickham at gmail.com
Tue Aug 13 16:24:50 CEST 2013


> 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.

Are you sure? That behaviour doesn't agree with the description of
method dispatch given in ?Methods, not with getClass("AB") which shows
that AB inherits from both A and B. (I totally agree that this is a
bad idea, and unlikely to be useful in real life, but I'm trying to
understand the details of S4 dispatch)

> getClass("AB")
Class "AB" [in ".GlobalEnv"]

Slots:

Name:  .xData
Class:   NULL

Extends:
Class "B", directly
Class "A", directly
Class ".NULL", by class "A", distance 2
Class "NULL", by class "A", distance 3, with explicit coerce
Class "OptionalFunction", by class "A", distance 4, with explicit coerce
Class "optionalMethod", by class "A", distance 4, with explicit coerce

-- 
Chief Scientist, RStudio
http://had.co.nz/



More information about the R-help mailing list