[Rd] callNextMethod() with builtin group methods fails to create proper environment

Stephen.Pope at ubs.com Stephen.Pope at ubs.com
Mon Dec 24 21:42:18 CET 2007

Hi all,

After all these years, I am finally porting some R-2.3.1-based S4 object
code to R-2.6.1, dealing with all the S4 object system changes that came
in R-2.4.0.

I've run across what appears to be some sort of ommission in the
implementation of callNextMethod() when used with primitives having
group generic methods.

In a stock R-2.6.1 patched (happens to be running on AMD64/RHEL 4, but
that shouldn't matter), the following occurs:

R version 2.6.1 Patched (2007-12-03 r43584)
Copyright (C) 2007 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

> options(error=quote(traceback()))
> setClass("Foo", contains="matrix")
[1] "Foo"
> setMethod("Compare", signature(e1="Foo", e2="Foo"), function(e1, e2) {
callNextMethod() })
[1] "Compare"
> f1 = new("Foo")
> f1 == f1
Error in get(fname, envir = envir) : variable ".nextMethod" was not
7: get(fname, envir = envir)
6: callGeneric(e1 at .Data, e2 at .Data)
5: .nextMethod(e1 = e1, e2 = e2)
4: .Call("R_nextMethodCall", call, callEnv, PACKAGE = "methods")
3: callNextMethod()
2: f1 == f1
1: f1 == f1

It appears that the issue occurs because the particular set of branches
through the logic of callNextMethod() which result because the next
method, which happens to be:

> selectMethod("Compare", signature("array","array"))
Method Definition:

function (e1, e2)
callGeneric(e1 at .Data, e2 at .Data)
<environment: 0x1582960>

        e1      e2
target  "array" "array"
defined "array" "array"

does not get the various "magic" variables such as .Generic, .Method,
etc. defined in its frame. Thus, callGeneric() fails when, failing to
find ".Generic" then takes the function symbol for the call (which
callNextMethod() has constructed to be ".nextMethod") and attempts to
look it up, which of course also fails, leading to the resulting error
seen above.

The obvious workaround is to avoid callNextMethod() when I know the next
method is one of these primitive group generics, replacing it with the
body of that generic method directly. Straightforward, but unfortunate.

stephen pope
Prediction Company LLC/UBS AG
stephen.pope at ubs.com

-------------- next part --------------
Visit our website at http://www.ubs.com

This message contains confidential information and is intended only 
for the individual named.  If you are not the named addressee you 
should not disseminate, distribute or copy this e-mail.  Please 
notify the sender immediately by e-mail if you have received this 
e-mail by mistake and delete this e-mail from your system.
E-mails are not encrypted and cannot be guaranteed to be secure or 
error-free as information could be intercepted, corrupted, lost, 
destroyed, arrive late or incomplete, or contain viruses.  The sender 
therefore does not accept liability for any errors or omissions in the 
contents of this message which arise as a result of e-mail transmission.  
If verification is required please request a hard-copy version.  This 
message is provided for informational purposes and should not be 
construed as a solicitation or offer to buy or sell any securities 
or related financial instruments.

More information about the R-devel mailing list