In the following code, 'multiply' doesn't multiply a...@x by 2. I'm
wondering how to define a method that can change the slot of a class.

$ Rscript setGeneric.R
> setClass(
+     Class='A',
+     representation=representation(
+         x='numeric'
+         )
+     )
[1] "A"
>
> setMethod(
+     f='initialize',
+     signature='A',
+     definition=function(.Object,x){
+       cat("~~~ A: initializator ~~~\n")
+       .Object<-callNextMethod(.Object,x=x)
+       return(.Object)
+     }
+     )
[1] "initialize"
>
> setGeneric(
+     name='multiply',
+     def=function(object){
+       standardGeneric('multiply')
+     }
+     )
[1] "multiply"
>
> setMethod(
+     f='multiply',
+     signature='A',
+     definition=function(object){
+       obj...@x=object@x*2
+     }
+     )
[1] "multiply"
>
> a=new(Class='A',x=10)
~~~ A: initializator ~~~
> multiply(a)
> print(a...@x)
[1] 10

______________________________________________
R-help@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.

Reply via email to