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

Thomas Koenig thomasio at cs.tu-berlin.de
Sat Jul 26 15:24:48 CEST 2003


Am Samstag, 26. Juli 2003 11:38 schrieb Peter Dalgaard BSA:
> Peter Dalgaard BSA <p.dalgaard at biostat.ku.dk> writes:
> > Prof Brian Ripley <ripley at stats.ox.ac.uk> writes:
> > > What are you trying to do with this?  Assignment (<-) is not a
> > > function,

But what the difference between <- and e.g. the function length or "[<-"? As I 
understood in "methods" everything has a class. And R says me with is(...) 
(hope that the results are correct):
< is(get("<-"))
[1] "function"         "OptionalFunction" "PossibleMethod"
< is(get("length"))
[1] "function"         "OptionalFunction" "PossibleMethod"
> is(get("[<-"))
[1] "function"         "OptionalFunction" "PossibleMethod"
> ## test for the correct result of get(...) ?
> x <- 10
> is(get("x"))
[1] "numeric" "vector"
> ## and
> setGeneric("<-")
[1] "<-"
Warning message:
"<-" is a primitive function; its generic definition is built in and 
automatically included. in: setGeneric("<-")

> > > 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.
> >
> > Eh? Are you sure about that???
> >
> > > quote("<-"(a,b))
> >
> > a <- b
>
> Adding on to this, I think the point is that assignment bypasses the
> usual *evaluation* rules, even though it is syntactically a binop.
>
> I think it basically has to be so: For one thing, it is kind of
> difficult to check for a signature match without evaluating the
> arguments and the left hand side of an assignment will not in general
> exist at that point.

In the methods is the class "missing". Could that help?
for one thing : signature=c("missing","A") (only allowed for <- ?)
for both things: signature=c("A","B")
for nothing ;-) : signature=c("missing","missing")

Thanks and Best Regards

Thomas König




More information about the R-help mailing list