Re: [R] Understanding S4 method dispatch

2013-08-14 Thread Hervé Pagès
And it doesn't even select "the first method lexicographically in the ordering" (whatever that means): setClass("B", "NULL") setClass("A", "NULL") setMethod("show", "B", function(object) cat("B object\n")) setMethod("show", "A", function(object) cat("A object\n")) setClass("AB", contai

Re: [R] Understanding S4 method dispatch

2013-08-14 Thread Hervé Pagès
Hi Zehnder, You're right that the fact that B already inherits from A is probably part of the story but it's not all the story: setClass("A", "NULL") setClass("B", "A") setMethod("show", "A", function(object) cat("A object\n")) setMethod("show", "B", function(object) cat("B object\n"))

Re: [R] Understanding S4 method dispatch

2013-08-14 Thread Hadley Wickham
On Wed, Aug 14, 2013 at 11:36 AM, Simon Zehnder wrote: > Because the signature is always (A,A) or (B,B). Then, as in AB we have A and > B and no relationship between A and B, R chooses the method > lexicographically. The result is as expected: f for A is chosen. It's not as expected, because it

Re: [R] Understanding S4 method dispatch

2013-08-14 Thread Simon Zehnder
Because the signature is always (A,A) or (B,B). Then, as in AB we have A and B and no relationship between A and B, R chooses the method lexicographically. The result is as expected: f for A is chosen. If you would do something like: setClass("A", contains = "list") setClass("B", contains = "

Re: [R] Understanding S4 method dispatch

2013-08-14 Thread Hadley Wickham
> In my opinion the reason for the behavior lies in the specific multiple > inheritance structure between AB, B and A. So what if we don't make such a weird inheritance structure, and instead have A and B inherit from a common parent: setClass("A", contains = "list") setClass("B", contains = "li

Re: [R] Understanding S4 method dispatch

2013-08-14 Thread Simon Zehnder
Ambiguity is indeed detected by R and the user is informed on it. But in the case of Hadley's example, I still believe, that the specific multiple inheritance structure creates this behavior. If you call: showMethods("f") Function: f (package .GlobalEnv) x="A", y="A" x="AB", y="AB" (inherite

Re: [R] Understanding S4 method dispatch

2013-08-13 Thread Hervé Pagès
Hi Hadley, I suspect that the dispatch algorithm doesn't realize that selection is ambiguous in your example. For 2 reasons: (1) When it does realize it, it notifies the user: setClass("A", "NULL") setGeneric("f", function(x, y) standardGeneric("f")) setMethod("f", sig

Re: [R] Understanding S4 method dispatch

2013-08-13 Thread Simon Zehnder
If you take an example which works with slots, setClass("A", representation(a = "numeric") setClass("B", contains = c("A"), representation(b = "numeric")) a <- new("A", a = 2) b <- new("B", a = 3, b = 2) setClass("AB", contains = c("A", "B")) new("AB", a = 2, b = 3) You see, that there is only o

Re: [R] Understanding S4 method dispatch

2013-08-13 Thread Hadley Wickham
> 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

Re: [R] Understanding S4 method dispatch

2013-08-13 Thread Simon Zehnder
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 objec