3

I am reading Martin Morgan's notes on reference classes, and on slide 7, he uses setMethod to define the show function without setting it previously as a generic using setGeneric.

Why is this allowed? Is there a list of methods that are declared as generic for all reference classes, including show?

tchakravarty
  • 10,736
  • 12
  • 72
  • 116
  • The slides were written when reference classes were new, or I was new to reference classes; these days there is automatic dispatch to a `show` method when an instance is printed: `F = setRefClass("F", methods=list(show=function() { cat(class(.self), "!\n") }))` – Martin Morgan Dec 17 '13 at 08:11
  • @MartinMorgan Thanks Martin. I wonder if there is newer material in reference classes or if there are packages that make substantial use of reference classes? – tchakravarty Dec 17 '13 at 09:22

2 Answers2

5

show is already a generic function:

> isGeneric("show")
[1] TRUE

A list of generics in your parent environment can be obtained by calling getGenerics:

> getGenerics()
An object of class "ObjectsWithPackage":

Object:  "-"    "!="   "["    "*"    "/"    "&"    "%/%"  "%%"   "^"    "+"   
Package: "base" "base" "base" "base" "base" "base" "base" "base" "base" "base"

Object:  "<"    "<="   "=="   ">"    ">="   "|"    "$"    "$<-"  "abs"  "acos"
Package: "base" "base" "base" "base" "base" "base" "base" "base" "base" "base"

Object:  "acosh" "addNextMethod" "Arith" "asin" "asinh" "atan" "atanh" "body<-"
Package: "base"  "methods"       "base"  "base" "base"  "base" "base"  "base"  

Object:  "cbind2"  "ceiling" "coerce"  "coerce<-" "Compare" "Complex" "cos" 
Package: "methods" "base"    "methods" "methods"  "methods" "base"    "base"

Object:  "cosh" "cummax" "cummin" "cumprod" "cumsum" "digamma" "exp"  "expm1"
Package: "base" "base"   "base"   "base"    "base"   "base"    "base" "base" 

Object:  "floor" "gamma" "initialize" "kronecker" "lgamma" "loadMethod" "log" 
Package: "base"  "base"  "methods"    "base"      "base"   "methods"    "base"

Object:  "log10" "log1p" "log2" "Logic" "Math" "Math2"   "Ops"  "rbind2" 
Package: "base"  "base"  "base" "base"  "base" "methods" "base" "methods"

Object:  "round" "show"    "sign" "signif" "sin"  "sinh" "slotsFromS3" "sqrt"
Package: "base"  "methods" "base" "base"   "base" "base" "methods"     "base"

Object:  "Summary" "tan"  "tanh" "trigamma" "trunc"
Package: "base"    "base" "base" "base"     "base" 
Scott Ritchie
  • 10,293
  • 3
  • 28
  • 64
  • So then, the generic methods that don't need to be set explicitly as generic methods for reference classes are `getGenerics()[getGenerics()@package=='methods']`? Or is it the entire list? – tchakravarty Dec 17 '13 at 05:31
  • I haven't actually used reference classes, so I'm not sure how they work. I've only looked at S3 and S4. I imagine they work much the same way as S4 classes though if they rely on generics. – Scott Ritchie Dec 17 '13 at 05:40
  • Hmm, ok, I don't see `cummax` being a generic for S4/reference classes, so probably only the ones from the package `methods`. Will test to see if that is true. Thanks for your answer, it gets me most ways home. – tchakravarty Dec 17 '13 at 05:43
  • Based on my skimming of: http://www.inside-r.org/r-doc/methods/ReferenceClasses, you're better off defining your own method anyway, i.e. myRefClass$cummax. The point being that Reference classes are like java: methods belong to classes, rather than methods having functions for each glass (and a generic). – Scott Ritchie Dec 17 '13 at 05:50
  • _"rather than methods having functions for each glass (and a generic)"_ You mean rather than (generic) functions having methods for each class? Also, I meant that there is no way `cummax` is a generic for all reference classes, hence the only ones that are generic for reference classes are the ones that belong to the package `methods`. – tchakravarty Dec 17 '13 at 08:21
  • Yes, I'm still bad at explaining the S4 system since I'm much more familiar with Java style OO. `cummax` is a generic (see `isGeneric("cummax")`), and its documentation (`?cummax`) implies that its there to have methods defined for it. – Scott Ritchie Dec 18 '13 at 00:31
  • So you could define a cummax method for your reference classes using `setMethod`, however I believe the reference class paradigm approach would be to define a `$cummax` method within each reference class. – Scott Ritchie Dec 18 '13 at 00:32
1

If you look at ?show you see that there is a default dispatch to showDefault. I seem to remember seeing a list of other methods that were presumed available, but I cannot find the link to them at the moment, so to your second question, I do not know.

IRTFM
  • 258,963
  • 21
  • 364
  • 487