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

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Jul 26 11:03:07 CEST 2003


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




More information about the R-help mailing list