Re: [R] How to change the value of a class slot

From: Uwe Ligges <ligges_at_statistik.uni-dortmund.de>
Date: Tue 07 Jun 2005 - 16:36:25 EST

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@i <- pm@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@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



R-help@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 Received on Tue Jun 07 16:46:21 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:26 EST