First, anyone planning to debug methods should consider the general form
of the trace() function with signature= to say what method to trace, and
some suitable interactive function such as "browser" or
"recover" to
examine the computations. See ?trace
In this example, something like:
trace("initialize", sig="Superclass", browser)
Now, the specific example. There are 3 special features used:
1. Nonstandard arguments for the initialize method (its official
arguments are (.Object, ...))
2. callNextMethod
3. within callNextMethod, providing explicit arguments. The simple case
is callNextMethod(), which passes on the arguments to the current method.
Turns out that it's the third step that finds a bug in the heuristics
used by callNextMethod to construct the actual call.
In your example, you don't need the explicit arguments since they just
replicate the formal arguments to initialize(). If you omit them, the
computation is simpler & works.
The bug can probably be fixed, but until 2.2 comes out at least, you
need to stick to the simpler callNextMethod().
Removing the extraneous cat() and str() calls, the revised example is:
------------
R> setClass("Superclass", representation(id =
"character"),
contains = "VIRTUAL")
[1] "Superclass"
R> setMethod("initialize", signature(.Object =
"Superclass"),
function(.Object, id = "") {
if (length(id) > 0) {
.Object at id <- i .... [TRUNCATED]
[1] "initialize"
R> setClass("Subclass", contains = "Superclass")
[1] "Subclass"
R> setMethod("initialize", signature(.Object =
"Subclass"),
function(.Object, ...) {
callNextMethod()
})
[1] "initialize"
R> Subclass <- function(id = "") {
new("Subclass", id = id)
}
R> new("Subclass", id = "test1")
An object of class ?Subclass?
Slot "id":
[1] "test1"
R> Subclass(id = "test2")
An object of class ?Subclass?
Slot "id":
[1] "test2"
-----------------------
Paul Roebuck wrote:
> 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)
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>