Re: [R] R.oo static field

2005-07-12 Thread Henrik Bengtsson

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 getField() and setField(). 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)
##
# 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)
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Re: [R] R.oo static field

2005-07-09 Thread Spencer Graves
  Since I've seen no replies to your question, I will offer brief 
comments that will certainly not meet your needs but might get you 
started.  I don't know what you mean by a static member of a class.  I 
just got 3 hits from RSiteSearch(static member of a class), but I 
don't know if any one of them would help you.

  You might get more replies if you prepare another post giving a very 
short toy example in the form of a very few R commands to help explain 
what you want to do and the deficiencies in what you tried, using the 
posting guide http://www.R-project.org/posting-guide.html; to help you 
prepare your question.  The archives contain much information on 
classes.  In particular, you might get something from Thomas Lumley's 
Programmers Nitch in the June 2004 R News.  I also recommend chapters 
4 and 5 in Venables and Ripley (2000) S Programming (Springer);  the 
polynomial class discussed in sec. 4.3 is implemented in R package 
polynom.  The vision for what classes have become in recent versions 
of S-Plus and R is outlined in Chambers (1998) Programming with Data 
(Springer).  However, the actual implementation in S-Plus and R were 
sufficiently different from that book, at least when I tried to read it 
a few years ago, that I found it somewhat frustrating.

  spencer graves

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.
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

-- 
Spencer Graves, PhD
Senior Development Engineer
PDF Solutions, Inc.
333 West San Carlos Street Suite 700
San Jose, CA 95110, USA

[EMAIL PROTECTED]
www.pdf.com http://www.pdf.com
Tel:  408-938-4420
Fax: 408-280-7915

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html