[Rd] S4 generating function

Paul Roebuck roebuck at odin.mdacc.tmc.edu
Fri Aug 5 10:09:51 CEST 2005


Can someone explain what the problem is when I use the
generating function? And how to get debug() to stop in
the Superclass initialize method?


---- source -----
setClass("Superclass",
         representation(id = "character"),
         contains = "VIRTUAL")

setMethod("initialize",
          signature(.Object = "Superclass"),
          function(.Object, id = "") {
              cat("initialize (Superclass)", "\n")
              if (length(id) > 0) {
                  cat("\tid =", id, "\n")
                  .Object at id <- id
              }
              .Object
          })

setClass("Subclass",
         contains = "Superclass")

setMethod("initialize",
          signature(.Object = "Subclass"),
          function(.Object, ...) {
              cat("initialize (Subclass)", "\n")
              cat("\t... =");str(list(...));cat("\n")
              callNextMethod(.Object, ...)
          })

Subclass <- function(id = "") {
    new("Subclass", id = id)
}

cat("*** Create class using new() ***\n")
str(new("Subclass", id = "test1"))

cat("*** Create class using generating function ***\n")
str(Subclass(id = "test2"))


---- output -----
*** Create class using new() ***
initialize (Subclass)
        ... =List of 1
 $ id: chr "test1"

initialize (Superclass)
        id = test1
Formal class 'Subclass' [package ".GlobalEnv"] with 1 slots
  ..@ id: chr "test1"
*** Create class using generating function ***
initialize (Subclass)
        ... =List of 1
 $ id: chr "test2"

initialize (Superclass)
Error in .local(.Object, ...) : Object "id" not found


Thanks

----------------------------------------------------------
SIGSIG -- signature too long (core dumped)



More information about the R-devel mailing list