What are you trying to do with this? Assignment (<-) is not a function, and the language grammar does not convert a <- b into "<-"(a, b) (as it would with the binary operator functions). You could call it that way, and then it will probably work. On Sat, 26 Jul 2003, Thomas Koenig wrote:> Hi, > > perhaps a little bit unusual: is it possible to use "<- " as generic function > with a new signature? > The following example doesn't work: > > isGeneric("<-") > [1] FALSE > > setClass("A",representation(x = "numeric")) > [1] "A" > > setClass("B",representation(x = "numeric")) > [1] "B" > > myAssign.A <- function(x,value) > + { return(x); } > > setReplaceMethod("",c("A","B"),myAssign.A) > [1] "<-" > > ## because > > ##> setReplaceMethod > > ##function (f, ...) > > ##setMethod(paste(f, "<-", sep = ""), ...) > > ## and > > isGeneric("<-") > [1] TRUE > > a <- new("A") > > b <- new("B") > > a <- b > > a > An object of class "B" > Slot "x": > numeric(0) > > ## should be a ? > > ## but selectMethod(...) works correct > > selectMethod("<-",c("A","B")) > Method Definition (Class "MethodDefinition"): > > function(x,value) > { > print("myAssign.default"); > return(x); > } > > Signatures: > x value > target "A" "B" > defined "A" "B" > > What happens with the setReplaceMethod(...) call? > > My version is > platform i686-pc-linux-gnu > arch i686 > os linux-gnu > system i686, linux-gnu > status > major 1 > minor 7.1 > year 2003 > month 06 > day 16 > language R > > Thank you. > > Thomas K?nig > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >-- 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
Hi, perhaps a little bit unusual: is it possible to use "<- " as generic function with a new signature? The following example doesn't work:> isGeneric("<-")[1] FALSE> setClass("A",representation(x = "numeric"))[1] "A"> setClass("B",representation(x = "numeric"))[1] "B"> myAssign.A <- function(x,value)+ { return(x); }> setReplaceMethod("",c("A","B"),myAssign.A)[1] "<-"> ## because > ##> setReplaceMethod > ##function (f, ...) > ##setMethod(paste(f, "<-", sep = ""), ...) > ## and > isGeneric("<-")[1] TRUE> a <- new("A") > b <- new("B") > a <- b > aAn object of class "B" Slot "x": numeric(0)> ## should be a ? > ## but selectMethod(...) works correct > selectMethod("<-",c("A","B"))Method Definition (Class "MethodDefinition"): function(x,value) { print("myAssign.default"); return(x); } Signatures: x value target "A" "B" defined "A" "B" What happens with the setReplaceMethod(...) call? My version is platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major 1 minor 7.1 year 2003 month 06 day 16 language R Thank you. Thomas K?nig
The model of assignment in R is pretty simple: (variable) <- (expression) Evaluate expression, store result in variable. This is core syntax. A compiled implementation of this would not involve any function call. The behaviour does not in any way depend on the prior value of (variable), indeed, there is no requirement that (variable) _have_ any value. func(arg, args) <- (expression) This is handled by macro-expansion to arg <- "func<-"(arg, args, value=(expression)) (If arg is not a variable, the expansion is applied recursively.) This involves a perfectly ordinary function call (which happens to have a funny name) and a perfectly ordinary assignment. The behaviour of "func<-" may depend on the value of arg; it need not, and in that case arg need not have a value, thanks to lazy evaluation. Note that "func<-" itself does not, as a rule, change any variable (other than its own). The change is done by the ordinary assignment to arg. Example: "+<-" <- function(lhs, rhs, value) value - rhs Now x + 1 <- 3 results in x being 2. "<-" already *is* as generic as it can possibly be; it does the right thing whatever the value of the right hand side and whatever the value (including none) of the left hand side. If you want a kind of assignment which does something different, then there is no compelling reason to (ab)use "<-" to do it; you could follow the example of ?assign and use a readable intention-revealing function name to do whatever it is that you want, and it could exploit lazy evaluation to determine the name of the variable(s) it is to affect. If anyone succeeded in making "<-" act like a normal operator and then made its behaviour depend on the value of its first argument, we would be left with no simple way to *initialise* a variable. Please, DON'T try to 'make "<-" generic'.