[R] How to change the value of a class slot
Uwe Ligges
ligges at statistik.uni-dortmund.de
Tue Jun 7 08:36:25 CEST 2005
Ross Boylan wrote:
> On Sat, Jun 04, 2005 at 07:08:56PM +0200, Martin Maechler wrote:
>
>> Ross> nextPath <- function(pm){ #pm is a CompletePathMaker
>> Ross> pm at i <- pm at i+as.integer(1)
>> Ross> [etc]
>>
>>If your nextPath function has 'pm' as its last statement it
>>will return the updated object, and if you call it
>>as
>> mypm <- nextPath(mypm)
>>
>>you are
>> 1) updating mypm
>> 2) in a proper S way (i.e. no cheating).
>>
>>Regards,
>>Martin
>
>
> Wow. This is almost the exact inverse of the usual object behavior,
> in which only the class itself can update the slots (aka instance
> variables). None of the methods of the class can update instances of
> the class persistently without the help of outsiders, and only
> outsiders can change the slot values.
>
> (Yes, I realize that using the idiom you suggest of returning a new
> object one can have only class methods actually fiddling with the
> slots.)
>
> The inability of a class method to change a class object without
> outside help seems unfortunate.
>
> It looks as if instances of class objects are best thought of as
> immutable once created.
Obviously, there are many definition of "object oriented" programming,
and yours seems to be different from the S4 definition.
I was going to answer your first question at first, but you have not
given enough details - in particular it was not clear to me why your
approach did not work. I assumed that you are assigning the new object
again, which is the S way. You have to think about scoping rules and it
will be clear that the approach you are expecting is not a clean one in S.
Uwe Ligges
> Ross
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list