[R] How to make "<-" generic?

Thomas Koenig thomasio at cs.tu-berlin.de
Sat Jul 26 11:47:33 CEST 2003


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




More information about the R-help mailing list