[Rd] OOP with Encapsulated Class Definitions

Charlotte Maia maiagx at gmail.com
Mon Nov 23 11:04:04 CET 2009


Hi all,

I'm seeking feedback (good, bad or indifferent) in regards to
developing (further) a new class system for R, that uses encapsulated
class definitions (i.e. the method definitions are literally inside
the body of the class definition).

A working (however very rough and poorly tested) system is available
in my R package "oosp" with documentation in the vignette
"oosp2_Encapsulated_Classes_in_R".

A snippet of a class definition follows:
(Here double is a method that doubles the value of the attribute x).

> MyObject = function (...)
{   MyObject = function (x) .$x = x
    double = function () .$x = .$x * 2
    mclass ()
}

A snippet of creating and using an object:

> myobject = MyObject (10)
> myobject$x
[1] 10
> myobject~double ()
> myobject$x
[1] 20

Here class definitions are functions, however there are other possibilities...
Priority has been given to space rather than speed, hence the
unorthodox method despatch, however again, there are other
possibilities...

Here is an example of inheritance:

> MySuperClass = function (...)
{   MySuperClass = function (x) .$x = x
    mymethod = function () cat (.$x, "\n")
    mclass ()
}

> MySubClass = function (...)
{   MySubClass = function (y) super (y)
    mclass (super="MySuperClass")
}

> myobj = MySubClass (10)
> myobj~mymethod ()
10


kind regards
Charlotte Maia
(first time poster)



More information about the R-devel mailing list