[Rd] OOP with Encapsulated Class Definitions

From: Charlotte Maia <maiagx_at_gmail.com>
Date: Mon, 23 Nov 2009 23:04:04 +1300


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)



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Mon 23 Nov 2009 - 10:14:44 GMT

This archive was generated by hypermail 2.2.0 : Mon 23 Nov 2009 - 14:20:43 GMT