[R] Understanding S4 method dispatch
Hervé Pagès
hpages at fhcrc.org
Wed Aug 14 02:11:24 CEST 2013
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", signature("A", "ANY"), function(x, y) "A-ANY")
setMethod("f", signature("ANY", "A"), function(x, y) "ANY-A")
a <- new("A")
Then:
> f(a, a)
Note: method with signature ‘A#ANY’ chosen for function ‘f’,
target signature ‘A#A’.
"ANY#A" would also be valid
[1] "A-ANY"
(2) When dispatch is ambiguous, the "first method lexicographically in
the ordering" should be selected (according to ?Methods). So it
should be A#A, not B#B.
So it looks like a bug to me...
Cheers,
H.
On 08/13/2013 06:08 AM, Hadley Wickham 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")
>
--
Hervé Pagès
Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024
E-mail: hpages at fhcrc.org
Phone: (206) 667-5791
Fax: (206) 667-1319
More information about the R-help
mailing list