[Rd] OOP with Encapsulated Class Definitions

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Mon Nov 23 12:20:32 CET 2009


On Mon, Nov 23, 2009 at 10:04 AM, Charlotte Maia <maiagx at gmail.com> wrote:
> 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...


[Warning: this message contains a large number of my opinions of
beauty and aesthetics of OO code, seriously biased by my years as a
perl then a python programmer]

 Wow. That's almost Pythonic in its beauty. I'd considered a system
like this a few years back after looking at S4 OO and having my head
a-splode. I didn't get very far beyond the design stage (certainly
didn't write any code).

 I'm not sure if '.' is a good alias for the object inside methods,
but it's as good as python's 'self' (which is purely convention) or
C++ 'this'', and shouldn't clash with anything, and reminds me of '.'
as the current directory in Unix... Okay, maybe it is a good alias!

 On the downside, good luck getting people using a fourth OO system in
R. Expect messages of the form "I've written 10,000 lines of code in
my package and S4 was good enough for me".

 Can objects have directly accessible attributes, or is every method a
function? In the above, can I do myobject$x = 99 and have it work? If
not, does that imply a raft of getter and setter functions for any
attribute?

 I must install and have a play later this week. I can probably answer
those questions...

+1

Barry



More information about the R-devel mailing list