[Rd] Reference Classes copy(shallow=FALSE) unexpected behavior.
Manuel Castejón Limas
manuel.castejon at unileon.es
Wed May 11 14:27:39 CEST 2011
Dear Hadley,
Thank you very much for your interest in the question proposed.
The Con class is a Reference Class. P and k are from class listCon.
I provide in the following lines a little more detail in order to be able
to reproduce the case.
#Class declaration
gCon <- setRefClass("Con", fields=list(from="ANY",weight="numeric"))
gListAMORE <- setRefClass("listAMORE", fields=list(.Data="list"))
gListCon <- setRefClass("listCon", contains="listAMORE")
# Let's create a few connections
con1 <- gCon$new(from=1, weight=1.1)
con2 <- gCon$new(from=2, weight=2.2)
con3 <- gCon$new(from=3, weight=3.3)
# And a list of connections
lcon <-gListCon$new()
lcon$.Data <- list(con1, con2, con3)
# At this point, lcon contains:
lcon$.Data
[[1]]
An object of class "Con"
<environment: 0x9a1534>
[[2]]
An object of class "Con"
<environment: 0xcd7ff0>
[[3]]
An object of class "Con"
<environment: 0xdca724>
# Let's copy lcon to k
k <- lcon$copy(shallow=FALSE)
# Now k is a new object but the cons are shared with lcon!
k$.Data
[[1]]
An object of class "Con"
<environment: 0x9a1534>
[[2]]
An object of class "Con"
<environment: 0xcd7ff0>
[[3]]
An object of class "Con"
<environment: 0xdca724>
Best regards
Manuel
El 11/05/11 14:00, "Hadley Wickham" <hadley at rice.edu> escribió:
>2011/5/10 Manuel Castejón Limas <manuel.castejon at unileon.es>:
>> Dear all,
>>
>> I've just discovered the 'Reference Classes'.
>> In a previous attempt ---a year ago--- to re-implement in a Object
>>Oriented
>> fashion the AMORE package using S4 classes I strongly felt the need of
>>such
>> capability. It's great to have the Reference Classes now available.
>>Along
>> with the discovery of the Rcpp package, this new programming paradigm
>>has
>> boosted my interest in rewriting that package.
>>
>> Nevertheless, I have found a surprising behavior in the
>>$copy(shallow=FALSE)
>> method. Let's have a look at the results which I believe are
>> self-explanatories. The ".Data" field is a list which contains objects
>>from
>> the "Con" class ---connections for what is worth---.
>
>What sort of class is the Con class? S4 or reference?
>
>Hadley
>
>
>--
>Assistant Professor / Dobelman Family Junior Chair
>Department of Statistics / Rice University
>http://had.co.nz/
>
More information about the R-devel
mailing list