[Phylobase-devl] setGeneric vs. setMethod

Thibaut Jombart jombart at biomserv.univ-lyon1.fr
Mon Jun 23 14:44:12 CEST 2008


Peter Cowan wrote:
> Hey all,
>
> I'd like to add a new generic 'reorder'.  I was looking through the
> generics and methods and is appears that for each function we each.
> For example the generic "hasNodeLabels" is coded in the following way.
>
> In methods-phylo4.R there exists:
>
> setMethod("hasNodeLabels", "phylo4", function(x) {
>     length(x at node.label) > 0
> })
>
> In the phylo4.R there exists:
>
> setGeneric("hasNodeLabels", function(x) {
>     standardGeneric("hasNodeLabels")
> })
>
>
> My understanding is that the setGeneric calls the method
> standardGeneric() to define the function.  However, my understanding
> is that these doesn't set the default, it uses the previously set
> 'hasNodeLabels'.  My question is why do we set Generics in this
> manner?  Couldn't we also do:
>
> setGeneric("hasNodeLabels", useAsDefault = function(x) {
>     length(x at node.label) > 0
> })
>
> (maybe also setting the group parameter, which I don't quite grok)
>
> Thanks
>
> Peter
> _______________________________________________
> Phylobase-devl mailing list
> Phylobase-devl at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/phylobase-devl
>
>
>   
Hi Peter,

I think the call to standardGeneric is required to set the dispatcher 
on, that is, to be sure that the right method is used with the right 
signature. I think useAsDefault can be used together with it:

## set a generic
setGeneric("giveAnInt",
    def=function(x) { standardGeneric("giveAnInt") },
    useAsDefault=function(x){
        if(is.integer(x)) return(x) else return(0)})

## set a method for characters, that return NA
setMethod("giveAnInt", "character", function(x) {return(NA)})

## give it a try
 > giveAnInt(1.1)
[1] 0
 > giveAnInt(as.integer(2))
[1] 2
 > giveAnInt("toto")
[1] NA

## all works as expected ##

Now try this (the version you mentionned):
 > setGeneric("giveAnInt",
    def=function(x) {
        if(is.integer(x)) return(x) else return(0)})

Warning message: ## warning is explicit enough ...
In .NonstandardGenericTest(body(fdef), name, stdGenericBody) :
  the supplied generic function definition for giveAnInt does not seem 
to call 'standardGeneric'; no methods will be dispatched!
## ... but still we try
 > setMethod("giveAnInt", "character", function(x) {return(NA)})
[1] "giveAnInt"
 > giveAnInt("toto")
[1] 0

## no dispatching !


Cheers,

Thibaut.
-- 
######################################
Thibaut JOMBART
CNRS UMR 5558 - Laboratoire de Biométrie et Biologie Evolutive
Universite Lyon 1
43 bd du 11 novembre 1918
69622 Villeurbanne Cedex
Tél. : 04.72.43.29.35
Fax : 04.72.43.13.88
jombart at biomserv.univ-lyon1.fr
http://biomserv.univ-lyon1.fr/%7Ejombart/
http://adegenet.r-forge.r-project.org/


More information about the Phylobase-devl mailing list