##############################
#Confusing R OO Concept
#Confusing R way to change a value
#What is inside an R's environment?
##############################
setClass("Test", representation(changed="vector"),prototype(changed=c()))
setGeneric("getChanged", function(object, …) standardGeneric("getChanged"))
setMethod("getChanged", signature("Test"), function(object){return (object@changed)})
##################################################
#after this the changed is not changed at all, because it is inside an method,
#but even its onw method?
#################################################
setGeneric("test1", function(object, …) standardGeneric("test1"))
setMethod("test1", signature("Test"), function(object){object@changed<-c(1,2,3,4,5)})
###################################
#can this function change the value after call? Still not!!!
##################################
setGeneric("test2", function(object, …) standardGeneric("test2"))
setMethod("test2", signature("Test"), function(object){testing(object)<-c(1,2,3,4,5,6,7,8)})
setGeneric("test3", function(object, …) standardGeneric("test3"))
setMethod("test3", signature("Test"), function(object){testing(object)<-c(6,7,8, 9, 10); object})
#####################################
#replace a value for a S4 class, are we really replacing it?
#####################################
setGeneric("testing<-", function(this,value, …) standardGeneric("testing<-"))
setReplaceMethod("testing",signature("Test", "vector"), function(this, value) { this@changed<- value; this})
setMethod("print", signature("Test"), function(x){ print(getChanged(x))})
##########################################
#now, let us test it. PLEASE NOTE, this is in your Global environment!
#########################################
t<-new("Test", changed=c(1,2))
test1(t); print(t)
test2(t); print(t)
test3(t); print(t)
t<-test3(t); print(t)
testing(t)<-c("Hello","Open","souce", "committee"); print(t)
#but this is little wierd…
print(test1(t))
print(test2(t))
print(test3(t))
print(testing(t)<-c("done"))