I defined a member function for an S4 class (programming language R) which is supposed to add elements to a list, but it does nothing:
setClass("ListHolder",
representation(
.list = "list"
),
prototype(
.list = list()
))
setGeneric("add",
function(this,i) standardGeneric("add")
)
setMethod("add",
signature(this="ListHolder",i="numeric"),
definition = function(this,i){
k <- length(this@.list)
this@.list[[k+1]] <- i
})
testListHolder <- function(){
lh <- new("ListHolder")
for(i in 1:10) add(lh,i)
print(lh@.list)
}
testListHolder()
This will print an empty list. What is going on here?