Hi, In the situation below the note issued by the dispatch algo doesn't seem right: setClass("A", representation(stuff="complex")) setAs("ANY", "A", function(from) new("A", stuff=as.complex(from))) > as(6, "A") An object of class "A" Slot "stuff": [1] 6+0i > as(6L, "A") Note: Method with signature ?numeric#A? chosen for function ?coerce?, target signature ?integer#A?. "ANY#A" would also be valid An object of class "A" Slot "stuff": [1] 6+0i First thing is that the note is in contradiction with the output of selectMethod (seems like it's the method with signature "ANY#A" that was chosen, not the method with signature "numeric#A"): > selectMethod("coerce", c("integer", "A")) Method Definition: function (from, to = "A", strict = TRUE) new("A", stuff = as.complex(from)) Signatures: from to target "integer" "A" defined "ANY" "A" Second, and more importantly, there is no need for a note in the first place because there is no ambiguity at all (the method for "numeric#A" itself is inherited from "ANY#A"): > selectMethod("coerce", c("numeric", "A")) Method Definition: function (from, to = "A", strict = TRUE) new("A", stuff = as.complex(from)) Signatures: from to target "numeric" "A" defined "ANY" "A" showMethod gives me an overview of the situation (which is much simpler/cleaner than what the note is suggesting): > showMethods("coerce") Function: coerce (package methods) from="ANY", to="A" ... from="integer", to="A" (inherited from: from="ANY", to="A") ... from="numeric", to="A" (inherited from: from="ANY", to="A") Finally, if I switch the order (i.e. if I do 'as(6L, "A")' before 'as(6, "A")'), I don't get the note. I get this with R 2.15.1 and a recent R devel (2012-09-22 r60777). It looks like a regression because I was not able to reproduce with R <= 2.14. Thanks, H. -- 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
The as() function does some non-standard stuff, and it appears in this example to set up methods for coerce() in a way that fools the disambiguation code (which is newer than the as() code, I think). Pending a rewrite of as() (not imminent) the Note has been deactivated for the coerce() generic. (rev 60859) Thanks for the catch. John On 10/1/12 3:20 PM, Herv? Pag?s wrote:> Hi, > > In the situation below the note issued by the dispatch algo doesn't > seem right: > > setClass("A", representation(stuff="complex")) > setAs("ANY", "A", function(from) new("A", stuff=as.complex(from))) > > > as(6, "A") > An object of class "A" > Slot "stuff": > [1] 6+0i > > > as(6L, "A") > Note: Method with signature ?numeric#A? chosen for function ?coerce?, > target signature ?integer#A?. > "ANY#A" would also be valid > An object of class "A" > Slot "stuff": > [1] 6+0i > > First thing is that the note is in contradiction with the output of > selectMethod (seems like it's the method with signature "ANY#A" that > was chosen, not the method with signature "numeric#A"): > > > selectMethod("coerce", c("integer", "A")) > Method Definition: > > function (from, to = "A", strict = TRUE) > new("A", stuff = as.complex(from)) > > Signatures: > from to > target "integer" "A" > defined "ANY" "A" > > Second, and more importantly, there is no need for a note in the first > place because there is no ambiguity at all (the method for "numeric#A" > itself is inherited from "ANY#A"): > > > selectMethod("coerce", c("numeric", "A")) > Method Definition: > > function (from, to = "A", strict = TRUE) > new("A", stuff = as.complex(from)) > > Signatures: > from to > target "numeric" "A" > defined "ANY" "A" > > showMethod gives me an overview of the situation (which is much > simpler/cleaner than what the note is suggesting): > > > showMethods("coerce") > Function: coerce (package methods) > from="ANY", to="A" > ... > from="integer", to="A" > (inherited from: from="ANY", to="A") > ... > from="numeric", to="A" > (inherited from: from="ANY", to="A") > > Finally, if I switch the order (i.e. if I do 'as(6L, "A")' before > 'as(6, "A")'), I don't get the note. > > I get this with R 2.15.1 and a recent R devel (2012-09-22 r60777). > It looks like a regression because I was not able to reproduce with > R <= 2.14. > > Thanks, > H. > >
Maybe Matching Threads
- Possible problem with S4 dispatch
- cache most-recent dispatch
- S4 method dispatch and namespaces: why is default method selected
- selectMethod() can fail to find methods in situations of multiple dispatch
- selectMethod() can fail to find methods in situations of multiple dispatch