difference between assigning S4 to a list with "[" vs. "[["

I'm missing something in how the [<- and the [[<- operators work with S4 objects. Here's a quick sample. Looks like the [<- operator is somehow diving one slot-level down in the assignment (apologies for the clumsy lingo). What is actually going on ?

> blist <- list()
> blist[[1]] <- mpfr(5,10)
> blist[2] <- mpfr(7,10)
> blist
[[1]]
1 'mpfr' number of precision  10   bits 
[1] 5

[[2]]
'mpfr1' 7

> slotNames(blist[[2]])
[1] "prec" "exp"  "sign" "d"   
> slotNames(blist[[1]])
[1] ".Data"

> mp1d <- blist[[1]]@.Data
> mp1d
[[1]]
'mpfr1' 5

> slotNames(mp1d[[1]])
[1] "prec" "exp"  "sign" "d"

Solution 1:

blist is a list. Assigning to blist[[1]] should always assign the object into that location. Assigning to blist[1] needs to convert the RHS into a length 1 list, then puts its entry into that position.

But the way Rmpfr::mpfr objects are created is as list objects with a formal class. So the conversion to list just drops the formal class. Here are the first two lines of the mpfr class definition:

setClass("mpfr", ## a *vector* of "mpfr1", i.e., multi-precision float numbers
         contains = "list", ## of "mpfr1" entries:

The contains = "list" part says that mpfr is derived from list.