Hello (Heinrich),

I did not know I could do this. It doesn't seem to be documented anywhere.
Thought this would be helpful to the fraction of the community using package
R.oo. Note the call of a setMethodS3 method, xOne, in the setConstructorS3.
This is extremely useful if xOne (in this case) is a very complex method
(that you always want to be called every time you create a new object). If I
have something wrong please let me know! (I'm about to implement this in a
large'ish program.) Great package for OOP programming!

Example 1:

setConstructorS3("ClassA", function() {
   this = extend(Object(), "ClassA",
   .x=NULL
  )

  this$xOne() #  this is useful!!!!!!!!!!!!!
  this
 })

setMethodS3("xOne", "ClassA", function(this,...) {
  this$.x = 1

})
setMethodS3("getX", "ClassA", function(this,...) {
  this$.x
})


So x is always 1:
> a = ClassA()
> a$x
[1] 1

If you are new to R.oo:  if you only want x to be 1 (I.e. xOne above is
simple) you should do something like this:

Example 2:

setConstructorS3("ClassA", function() {
   this = extend(Object(), "ClassA",
   .x=1
  )
  this
 })

setMethodS3("getX", "ClassA", function(this,...) {
  this$.x
})

> a = ClassA()
> a$x
[1] 1

The following further illustrates what you can do with Example 1 above:

Example 3:

setConstructorS3("ClassA", function() {
   this = extend(Object(), "ClassA",
   .x=NULL,
   .y=1
  )
  this$xOne()
  this$xPlusY()
  this
 })
setMethodS3("xOne", "ClassA", function(this,...) {
  this$.x = 1

})
setMethodS3("xPlusY", "ClassA", function(this,...) {
  this$.x = this$.x + this$.y

})
setMethodS3("getX", "ClassA", function(this,...) {
  this$.x
})

> a = ClassA()
> a$x
[1] 2

Hope that helps!

Ben

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to