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

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Jul 28 16:29:19 CEST 2003

John Chambers <jmc at research.bell-labs.com> writes:

> - not evaluating the left side of the operator.  This is no problem in
> principle, since generic functions can have a signature (see
> ?setGeneric); if the signature omits the first argument, methods can be
> dispatched without evaluating that argument.

Yes, but if you want to dispatch *on* the first argument, you're in
trouble. AFAI understood, that was what the orig.poster wanted to do:
If a has class "A", ensure that a <- b leaves a as class "A" even
though b has class "B".
> - As Richard O'Keefe points out, replacement expressions (dim(x) <-
> NULL, e.g.) are handled as simple assignments of the result of
> "replacement functions" (x <- "dim<-"(x, NULL)).  In fact this means
> that defining methods for "<-" would catch ALL assignments.  Replacement
> functions are not methods but essentially a syntactic shorthand and a
> way of defining replacement operations in a language that doesn't allow
> pointers.

[Except, as you all of course know, that this is internally optimized
so as not to be quite true. E.g. x[225000] <- 1 does not create an
extra vector of 225000 or more cells, whereas (I think) x <-
"[<-"(x,225000,1) will do so. Luke knows much more about these sorts of

   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907

More information about the R-help mailing list