My question is when the object argument of NexthMethod be used? In the following example, weather object argument is used will not affects the result. ### foo=function(x) {UseMethod("foo")} foo.cls1=function(x) { x=x+1;class(x)<-"ncls" NextMethod() } foo.ncls=function(x) { cat("ncls\n") } foo.cls2=function(x) { cat("cls2\n");print(x) } a=1;class(a)=c("cls1","cls2")> foo(a)cls2 [1] 2 attr(,"class") [1] "ncls" ###> foo=function(x) {UseMethod("foo")} > > foo.cls1=function(x)+ { + x=x+1;class(x)<-"ncls" + NextMethod(,x) + }> > foo.ncls=function(x)+ { + cat("ncls\n") + }> > foo.cls2=function(x)+ { + cat("cls2\n");print(x) + }> > a=1;class(a)=c("cls1","cls2") > > foo(a)cls2 [1] 2 attr(,"class") [1] "ncls" Thank you very much. -- ?????? Deparment of Sociology Fudan University
In section 5.5 of the language manual it says: "It is important to realize that the choice of the next method depends on the current values of .Generic and .Class and not on the object. So changing the object in a call to NextMethod affects the arguments received by the next method but does not affect the choice of the next method." I think this needs to be put into ?NextMethod too since the current wording in ?NextMethod appears to contract the language manual: "object: an object whose class will determine the method to be dispatched. Defaults to the first argument of the enclosing function." At any rate, try using .Class to direct it to the appropriate method like this:> foo <- function(x) UseMethod("foo") > foo.cls1 <- function(x) { x <- x+1; .Class <- class(x) <- "ncls"; NextMethod() } > foo.ncls <- function(x) cat("ncls\n") > foo.cls2=function(x) { cat("cls2\n"); print(x) } > a <- 1; class(a) <- c("cls1","cls2") > > foo(a)ncls On 4/14/06, ronggui <ronggui.huang at gmail.com> wrote:> My question is when the object argument of NexthMethod be used? > > In the following example, weather object argument is used will not > affects the result. > > ### > foo=function(x) {UseMethod("foo")} > > foo.cls1=function(x) > { > x=x+1;class(x)<-"ncls" > NextMethod() > } > > foo.ncls=function(x) > { > cat("ncls\n") > } > > foo.cls2=function(x) > { > cat("cls2\n");print(x) > } > > a=1;class(a)=c("cls1","cls2") > > > foo(a) > cls2 > [1] 2 > attr(,"class") > [1] "ncls" > > ### > > foo=function(x) {UseMethod("foo")} > > > > foo.cls1=function(x) > + { > + x=x+1;class(x)<-"ncls" > + NextMethod(,x) > + } > > > > foo.ncls=function(x) > + { > + cat("ncls\n") > + } > > > > foo.cls2=function(x) > + { > + cat("cls2\n");print(x) > + } > > > > a=1;class(a)=c("cls1","cls2") > > > > foo(a) > cls2 > [1] 2 > attr(,"class") > [1] "ncls" > > Thank you very much. > > -- > ?????? > Deparment of Sociology > Fudan University > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >
?? 06-4-14??Gabor Grothendieck<ggrothendieck at gmail.com> ??????> In section 5.5 of the language manual it says: > > "It is important to realize that the choice of the next method depends on the > current values of .Generic and .Class and not on the object. So changing > the object in a call to NextMethod affects the arguments received by > the next method but does not affect the choice of the next method."Can anyone give an example to demostrated "affects the arguments received by the next method"?In my example NextMethod("foo") and NextMethod("foo",x) give the same result too.> foo=function(x) {UseMethod("foo")} > > foo.cls1=function(x)+ { + x=x+1 + NextMethod("foo") + }> > foo.ncls=function(x)+ { + cat("ncls\n") + }> > foo.cls2=function(x)+ { + cat("cls2\n");print(x) + }> > a=1;class(a)=c("cls1","cls2") > > foo(a)cls2 [1] 2 attr(,"class") [1] "cls1" "cls2"> foo=function(x) {UseMethod("foo")} > > foo.cls1=function(x)+ { + x=x+1 + NextMethod("foo",x) + }> > foo.ncls=function(x)+ { + cat("ncls\n") + }> > foo.cls2=function(x)+ { + cat("cls2\n");print(x) + }> > a=1;class(a)=c("cls1","cls2") > > foo(a)cls2 [1] 2 attr(,"class") [1] "cls1" "cls2"> I think this needs to be put into ?NextMethod too since the current > wording in ?NextMethod appears to contract the language manual: > > "object: an object whose class will determine the method to be > dispatched. Defaults to the first argument of the enclosing > function." > > At any rate, try using .Class to direct it to the appropriate method like this: > > > foo <- function(x) UseMethod("foo") > > foo.cls1 <- function(x) { x <- x+1; .Class <- class(x) <- "ncls"; NextMethod() } > > foo.ncls <- function(x) cat("ncls\n") > > foo.cls2=function(x) { cat("cls2\n"); print(x) } > > a <- 1; class(a) <- c("cls1","cls2") > > > > foo(a) > ncls > > On 4/14/06, ronggui <ronggui.huang at gmail.com> wrote: > > My question is when the object argument of NexthMethod be used? > > > > In the following example, weather object argument is used will not > > affects the result. > > > > ### > > foo=function(x) {UseMethod("foo")} > > > > foo.cls1=function(x) > > { > > x=x+1;class(x)<-"ncls" > > NextMethod() > > } > > > > foo.ncls=function(x) > > { > > cat("ncls\n") > > } > > > > foo.cls2=function(x) > > { > > cat("cls2\n");print(x) > > } > > > > a=1;class(a)=c("cls1","cls2") > > > > > foo(a) > > cls2 > > [1] 2 > > attr(,"class") > > [1] "ncls" > > > > ### > > > foo=function(x) {UseMethod("foo")} > > > > > > foo.cls1=function(x) > > + { > > + x=x+1;class(x)<-"ncls" > > + NextMethod(,x) > > + } > > > > > > foo.ncls=function(x) > > + { > > + cat("ncls\n") > > + } > > > > > > foo.cls2=function(x) > > + { > > + cat("cls2\n");print(x) > > + } > > > > > > a=1;class(a)=c("cls1","cls2") > > > > > > foo(a) > > cls2 > > [1] 2 > > attr(,"class") > > [1] "ncls" > > > > Thank you very much. > > > > -- > > ?????? > > Deparment of Sociology > > Fudan University > > > > > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > > > >-- ?????? Deparment of Sociology Fudan University
On Fri, 14 Apr 2006, ronggui wrote:> My question is when the object argument of NexthMethod be used?Looking at the C code, I don't think it is. But look at do_nextmethod in src/main/objects.c yourself. Note that in your examples it would not make any difference as 'x' is re-evaluated in the current frame, and so passing it explicitly will find the same value. As far as I can tell, in NextMethod(generic, object, ...), 'generic' is only used if not invoked via a generic, and only named arguments in ... are used, to replace or append to the argument list of the current method. This is an area where R-lang is only a draft, and the definitive documentation is the C code. The lack of precision seems not to be doing anyone much harm, as it is normally inadvisable to use NextMethod if you want to do anything convoluted (especially if you want it to work in both S and R).> In the following example, weather object argument is used will not > affects the result. > > ### > foo=function(x) {UseMethod("foo")} > > foo.cls1=function(x) > { > x=x+1;class(x)<-"ncls" > NextMethod() > } > > foo.ncls=function(x) > { > cat("ncls\n") > } > > foo.cls2=function(x) > { > cat("cls2\n");print(x) > } > > a=1;class(a)=c("cls1","cls2") > >> foo(a) > cls2 > [1] 2 > attr(,"class") > [1] "ncls" > > ### >> foo=function(x) {UseMethod("foo")} >> >> foo.cls1=function(x) > + { > + x=x+1;class(x)<-"ncls" > + NextMethod(,x) > + } >> >> foo.ncls=function(x) > + { > + cat("ncls\n") > + } >> >> foo.cls2=function(x) > + { > + cat("cls2\n");print(x) > + } >> >> a=1;class(a)=c("cls1","cls2") >> >> foo(a) > cls2 > [1] 2 > attr(,"class") > [1] "ncls" > > Thank you very much. > > -- > ?????? > Deparment of Sociology > Fudan University > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Maybe Matching Threads
- directives to explicitely exclude objects from import into namespaces
- the difference between UseMethod and NextMehod?
- NextMethod
- Do *not* pass '...' to NextMethod() - it'll do it for you; missing documentation, a bug or just me?
- NextMethod() and argument laziness