Hi, I'm usually confused about when to use 'slot' or '@'. I've frequently read that it's always preferable to use accessor functions, so I would think the '@' operator should be avoided. However, ?slot contains the following advise: "Generally, the only reason to use the functional form rather than the simpler operator is _because_ the slot name has to be computed." How do we decide whether to use the function or the operator? Cheers, -- Seb
I think the point is that if you use @ and then later redesign the internals of the class so that that component is not longer stored but is, instead, computed then @ will no longer be possible to use in that instance and a method call will be required which means that the internal change implies that the user interface must change. Of course, that is undesirable. In some programming languages like Python one can access a variable and call a function using the exact same syntax so that it would be possible to make such an internal change without affecting the user. On 9/26/06, Sebastian P. Luque <spluque at gmail.com> wrote:> Hi, > > I'm usually confused about when to use 'slot' or '@'. I've frequently > read that it's always preferable to use accessor functions, so I would > think the '@' operator should be avoided. However, ?slot contains the > following advise: > > > "Generally, the only reason to use the functional form rather than the > simpler operator is _because_ the slot name has to be computed." > > > How do we decide whether to use the function or the operator? > > > Cheers, > > -- > Seb > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
I think slot() is only necessary in the case where you have a <- "myslot" slot(object, a) which is equivalent to object at myslot It seems unlikely that you would use slot() during interactive use, but perhaps more so in programming. Even still, I rather infrequently find the need for slot() because classes have defined slot names---if you're going to access a slot, just use the name that you gave it in the class definition since that doesn't change from instance to instance. -roger Sebastian P. Luque wrote:> Hi, > > I'm usually confused about when to use 'slot' or '@'. I've frequently > read that it's always preferable to use accessor functions, so I would > think the '@' operator should be avoided. However, ?slot contains the > following advise: > > > "Generally, the only reason to use the functional form rather than the > simpler operator is _because_ the slot name has to be computed." > > > How do we decide whether to use the function or the operator? > > > Cheers, >-- Roger D. Peng | http://www.biostat.jhsph.edu/~rpeng/
I believe that the "accessor" functions you've read about are not the slot() function, rather they are functions specifically designed for extracting certain data from an object. An example is the 'coef()' extractor function, which extracts coefficients from many model objects. The name of the accessor function need not bear any resemblance to the actual name of the slot in which the data is stored, and indeed, there does not even need to be a one-to-one relationship between accessor functions and slots. AFAIK, slot() and '@' can be regarded as syntactic variants (please correct me if I'm wrong). -- Tony Plate Sebastian P. Luque wrote:> Hi, > > I'm usually confused about when to use 'slot' or '@'. I've frequently > read that it's always preferable to use accessor functions, so I would > think the '@' operator should be avoided. However, ?slot contains the > following advise: > > > "Generally, the only reason to use the functional form rather than the > simpler operator is _because_ the slot name has to be computed." > > > How do we decide whether to use the function or the operator? > > > Cheers, >