[R] How to change the value of a class slot
Ross Boylan
ross at biostat.ucsf.edu
Mon Jun 6 22:35:17 CEST 2005
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.
Ross
More information about the R-help
mailing list