[R] R.oo static field

Henrik Bengtsson hb at maths.lth.se
Tue Jul 12 13:36:08 CEST 2005


Omar Lakkis wrote:
> How can I define a static member of a class? not a static method,
> rather a static field that would be accessed by all instances of the
> class.

To define a static field in an Object class (the R.oo package), I 
recommend you to use a private field <.field> and then create a virtual 
field <field> by defining methods get<Field>() and set<Field(). These 
methods should get and set the field <.field> of the _static_ instance 
of the class. Below is an example, which also modifies the static field 
already in the constructor something which otherwise may be tricky. I 
will add this example to the help pages of R.oo in the next release.

Hope it helps

Henrik Bengtsson (author of R.oo)

######################################################################
# Example illustrating how to "emulate" static fields using virtual
# fields, i.e. get- and set-methods.  Here we use a private static
# field '.count' of the static class instance 'MyClass', i.e.
# MyClass$.count.  Then we define a virtual field 'count' via method
# getCount() to access this static field.  This will make all queries
# for 'count' of any object to use the static field instead.  In the
# same way is assignment controlled via the setCount() method.  A
# side effect of this way of coding is that all MyClass instances will
# also have the private field '.count' (set to zero except for the
# static field that is).
######################################################################
library(R.oo)

setConstructorS3("MyClass", function(...) {
   # Create an instance (the static class instance included)
   this <- extend(Object(), "MyClass",
     .count = 0
   )

   # In order for a static field to be updated in the
   # constructor it has to be done after extend().
   this$count <- this$count + 1;

   # Return the object
   this;
})


setMethodS3("as.character", "MyClass", function(this, ...) {
   paste(class(this)[1], ": Number of instances: ", this$count, sep="");
})


# Get virtual field 'count', e.g. obj$count.
setMethodS3("getCount", "MyClass", function(this, ...) {
   MyClass$.count;
})


# Set virtual field 'count', e.g. obj$count <- value.
setMethodS3("setCount", "MyClass", function(this, value, ...) {
   MyClass$.count <- value;
})


# Create four instances of class 'MyClass'
obj <- lapply(1:4, MyClass)
print(obj)
print(MyClass$count)
print(obj[[1]]$count)
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: MyClass.R
Url: https://stat.ethz.ch/pipermail/r-help/attachments/20050712/ecfaa653/MyClass.pl


More information about the R-help mailing list