[R] From Java to R OOP

R. Michael Weylandt michael.weylandt at gmail.com
Mon Mar 25 11:03:38 CET 2013


On Mon, Mar 25, 2013 at 6:51 AM, Francisco J. Bido <bido at mac.com> wrote:
> Hi, I'm new to OOP in R so please forgive the naiveness of some of the questions.  Here are a couple of them.  It would be great if you can contrast to OOP in Java.

Java is not the end-all of OOP (in fact S is a good bit older than
Java) and you might find that the Lisp or Dylan object systems are a
better analogy. (I'm only going by hearsay on Dylan; never used it
myself) You might also quickly breeze through:
https://github.com/hadley/devtools/wiki/S3

>
> 1. R's S4 appears to centered around a dispatch mechanism which in my understanding is just a way to implement polymorphism. Now, here's the snag, I thought polymorphism was an aspect of OOP not by itself the definition of OOP.  What am I missing here?  Is any language that implements polymorphism automatically OO?
>

If you accept the immutability of objects, then arguably yes, I
suppose polymorphism gives you a great deal of it. The remaining
weaknesses are generally addressed by the S4 object system.

(Not immutability of bindings like Haskell, but the fact that x <- y
<- 1:5; y[3] <- 10 won't change x. In theory this is done by creating
a new y with the modified 3rd element and binding the name y to that;
not entirely thus in practice for performance reasons )

> 2. Can someone provide a simple example of how NextMethod() works?  I read some things about but I can't make any sense out of it.
> It's supposed to facilitate inheritance but how?  Why is it needed, what happens if it's ignored? An example would be useful. Is there a Java equivalent of NextMethod()?

Grepping through R's source, it seems that the print system uses a
fair amount of NextMethod for the AsIs and noquote print methods. You
might take a look at those: also, section 7 of
http://cran.r-project.org/doc/manuals/r-devel/R-exts.html

MW


>
> Many Thanks!
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list