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

From: Ross Boylan <ross_at_biostat.ucsf.edu>
Date: Tue 07 Jun 2005 - 06:35:17 EST

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.

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 Received on Tue Jun 07 06:43:50 2005

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