Skip to content

S4 Method Signatures

4 messages · Martin Maechler, Dario Strbenac

#
Hello,

If the signature of a method defines which generic it implements then I'm confused about why this minimal example I invented won't work :

setGeneric("myFun", function(rs, ...){standardGeneric("myFun")})
setGeneric("myFun", function(cs, ...){standardGeneric("myFun")})

setMethod("myFun", "numeric", function(rs, colour = "Blue")
{
	cat(rs*100, colour)
})

setMethod("myFun", "character", function(cs, colour = "Red")
{
	cat(cs, colour)
})

Thanks for any tips,
                    Dario.

--------------------------------------
Dario Strbenac
Research Assistant
Cancer Epigenetics
Garvan Institute of Medical Research
Darlinghurst NSW 2010
Australia
#
DS> Hello,
    DS> If the signature of a method defines which generic it implements then I'm confused about why this minimal example I invented won't work :

very short answer:

     if(FALSE) {  0 == 1 }

slightly longer:

     if( something_wrong )  {  anything_can_be_put_here }

In other words:

Your assumption is wrong.
There's only one generic and potentially many methods, defined
via signatures, but you have not understood what a signature is.

    DS> setGeneric("myFun", function(rs, ...){standardGeneric("myFun")})
    DS> setGeneric("myFun", function(cs, ...){standardGeneric("myFun")})

    DS> setMethod("myFun", "numeric", function(rs, colour = "Blue")
    DS> {
    DS> cat(rs*100, colour)
    DS> })

    DS> setMethod("myFun", "character", function(cs, colour = "Red")
    DS> {
    DS> cat(cs, colour)
    DS> })

Rather:

setGeneric("myFun", function(x, ...) standardGeneric("myFun"))

setMethod("myFun", "numeric", 
          function(x, colour = "Blue") cat(xs*100, colour))

## etc

where the last is an abbreviated form of 

setMethod("myFun", signature(x = "numeric"), 
          function(x, colour = "Blue") cat(xs*100, colour))

which is abbreviated for

setMethod("myFun", signature = signature(x = "numeric"), 
          function(x, colour = "Blue") cat(xs*100, colour))



I do wonder why the examples on the  ?setMethod   
help page where not sufficient here..

    DS> Thanks for any tips,

you're welcome.
Martin Maechler, ETH Zurich


    DS> --------------------------------------
    DS> Dario Strbenac
    DS> Research Assistant
    DS> Cancer Epigenetics
    DS> Garvan Institute of Medical Research
    DS> Darlinghurst NSW 2010
    DS> Australia

    DS> ______________________________________________
    DS> R-devel at r-project.org mailing list
    DS> https://stat.ethz.ch/mailman/listinfo/r-devel
DS> Hello, If the signature of a method defines which
    DS> generic it implements then I'm confused about why this
    DS> minimal example I invented won't work :

    DS> setGeneric("myFun", function(rs,
    DS> ...){standardGeneric("myFun")}) setGeneric("myFun",
    DS> function(cs, ...){standardGeneric("myFun")})

    DS> setMethod("myFun", "numeric", function(rs, colour =
    DS> "Blue") { cat(rs*100, colour) })

    DS> setMethod("myFun", "character", function(cs, colour =
    DS> "Red") { cat(cs, colour) })

    DS> Thanks for any tips, Dario.

    DS> -------------------------------------- Dario Strbenac
    DS> Research Assistant Cancer Epigenetics Garvan Institute
    DS> of Medical Research Darlinghurst NSW 2010 Australia

    DS> ______________________________________________
    DS> R-devel at r-project.org mailing list
    DS> https://stat.ethz.ch/mailman/listinfo/r-devel
1 day later
#
Ah yes. I should have checked the details more finely. I come from a
programming background in a different language, and had the thought that
generic functions would work much like function overloading.