[R] Question about R.oo package

Henrik Bengtsson hb at biostat.ucsf.edu
Tue Jun 28 02:53:33 CEST 2011


Hi W. Kaisers,

sorry for the delay - I just spotted you question:

On Thu, Jun 16, 2011 at 5:52 AM,  <Kaisers at med.uni-duesseldorf.de> wrote:
> Dear R.oo package users,
>
> while testing some functionality of the R.oo, I found that during the first construction of a object from a class, the constructor is twice called, but only one object is finalized.
>
> In all subsequent creation processes, the constructor is (expectedly) called once.

Yes, this is the expected behavior.  The reason is that the first time
the constructor is called, a static instance of the class is also
created (which is done by calling the constructor yet again without
arguments).  There is only one static instance of a class, so any
following calls to the constructor will not spawn a second call.

Related questions have been asked before and my answers then may
clarify this further, e.g.

[R] First call to constructor fails (R.oo), December 10, 2008
https://stat.ethz.ch/pipermail/r-help/2008-December/182212.html

[R] Help with R.oo, July 17, 2009
https://stat.ethz.ch/pipermail/r-help/2009-July/204951.html

Hope this helps

Henrik

> Here some example code:
>
> library(R.oo)
> setConstructorS3("Test_class",function(val) {
>                                cat("constructor\n")
>                                if(missing(val)) val<-NA;
>                                extend(Object(), "Test_class", .val=val) })
> setMethodS3("finalize","Test_class",function(this,...) { cat("destructor\n") })
> o<-Test_class(1)
> rm(o)
> gc()
>
> Is there any explanation for this behaviour?
>
> Thanks very much in advance
>
> W. Kaisers
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list