From jmc4 at stanford.edu  Thu Nov 11 01:10:26 2010
From: jmc4 at stanford.edu (John Chambers)
Date: Wed, 10 Nov 2010 16:10:26 -0800
Subject: [Rd] callNextMethod fails with "$" generic
In-Reply-To: <cpnaalhfink.fsf@gmail.com>
References: <cpnaalhfink.fsf@gmail.com>
Message-ID: <4CDB3472.1010300@stanford.edu>

The problem here is that the primitive for `$` does not use standard R 
evaluation on its second argument, so when it is selected as the next 
method the call is effectively x$name regardless of the original call.

If possible, I would avoid such cascaded calls of methods for `$`, 
precisely because of the nonstandard evaluation, which makes the 
interpretation of each level of the call ambiguous.

If this kind of code is needed, then the primitive method  should get a 
call in which the literal name is in place and the object has been 
coerced suitably.

In this example, something like

 > setMethod("$", "mylist", function(x, name) {
+     theName <- substitute(name)
+     expr <- substitute(xx$NAME, list(NAME = theName))
+     xx <- unclass(x)
+     eval(expr)
+ })
[1] "$"
 > tl <- new("mylist")
 > tl[["z"]] <- 1
 > tl$z
[1] 1

On 11/10/10 4:49 AM, Vitalie S. wrote:
> Dear Developers,
>
> callNextMethods does not work with "$"
>
> setClass("mylist", contains = "list"):
> setMethod("$",
>            signature(x = "mylist"),
>            function (x, name){
>                cat("here:\n")
>                callNextMethod()
>            })
>
> tl<- new("mylist")
> tl[["x"]]<- 343
>
> tl$x
> #here:
> #NULL
>
>
> If I use callNextMethod(x=x, name=name)
>
> this error is issued:
>
> Error in function (classes, fdef, mtable)  :
>    unable to find an inherited method for function "addNextMethod", for signature "function"
>
> It must be something "$" specific. If the above is an expected behavior , how
> should I call next method for "$" generic?
>
> My info:
> R version 2.12.0 Patched (2010-11-01 r53513)
> Platform: i386-pc-mingw32/i386 (32-bit)
>
> Same behavior for official R 2.12.0.
>
> Thanks,
> Vitalie.
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



[Rd] callNextMethod fails with "$" generic
John Chambers