hadley/adv-r

S4 ambiguous method

CrossD opened this issue · 0 comments

When an S4 class has two (immediate) parent classes, the two classes are regarded as ordered, and the distance to the first class is shorter than to the 2nd class. Thus, the way S4 disambiguous method is not entirely like described in Ch 15. The second figure regards the superclasses as un-ordered, which is what current implementation (I am on R 4.2.1) reflects:

> setClass("A", slots=c(a  = "numeric"))
> setClass("B", slots=c(b  = "numeric"))
> setClass("AB", contains=c("A", "B"))
> setGeneric("myg", function(object) standardGeneric("myg"))
[1] "myg"
> 
> setMethod("myg", "A", function(object) 1)
> setMethod("myg", "B", function(object) 2)
> ab <- new("AB", a=1, b=2)
> myg(ab) # No warning of ambiguity
[1] 1