R.oo question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

R.oo question

Omar Lakkis
This is a simple R.oo question but I, thankfully, hope that someone
would explain it to me so I would better understand this work frame.
I create this class:

setConstructorS3("MyExample", function(param=0) {
  print(paste("called with param=", param))
  extend(Object(), "MyExample",
    .param = param
  );
})

>From what is printed out, who made the second call to the class with
the default param?

> MyExample(1)
[1] "called with param= 1"
[1] "called with param= 0"                # <- this is line in question
[1] "MyExample: 0x02831708"

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: R.oo question

Henrik Bengtsson
On 5/26/06, Omar Lakkis <[hidden email]> wrote:

> This is a simple R.oo question but I, thankfully, hope that someone
> would explain it to me so I would better understand this work frame.
> I create this class:
>
> setConstructorS3("MyExample", function(param=0) {
>   print(paste("called with param=", param))
>   extend(Object(), "MyExample",
>     .param = param
>   );
> })
>
> >From what is printed out, who made the second call to the class with
> the default param?
>
> > MyExample(1)
> [1] "called with param= 1"
> [1] "called with param= 0"                # <- this is line in question
> [1] "MyExample: 0x02831708"

That is because of a new US government rule requiring that one
instance of every R.oo class is forwarded to the NSA.

Seriously, you will see that this happens once and only once for each
class defined this way and it normally happens when you create your
first instance (otherwise before that).  The first call to the
constructor creates a so called static instance of the class.  The
static instance is for instance used when you type 'MyExample' to get
the API coupled to class MyExample.  Another example:

> Object
Object
  public $(name)
  public $<-(name, value)
  public [[(name)
  ...
  public objectSize(...)
  public print(...)
  public save(file=NULL, path=NULL, compress=TRUE, ...)
}

Technical details: The static instance is not always needed, but quite
often.  If needed, it has to be create at some stage and I found that
having extend() to do this is quite convenient.  The alternative would
be to let you do it explicitly, e.g. getStaticInstance(Object).  Note
that there is no central registry/database keeping track of the
classes defined by R.oo/Object, but all is just plain S3 classes
making use of the S3/UseMethod dispatching mechanisms - that's all.

There is a low-level way to figure out if the call to the constructor
is for the static instance or not, but normally it is easier not to
output anything in the constructor.  If you want to know the low-level
way, I'll have have to get back to, because I don't know it off the
top of my head.  I might provide a method for this if there is a need
for it, e.g. hasStaticInstance(MyExample).

Cheers

Henrik
(the author)

> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Loading...