Skip to content
Prev 40015 / 63421 Next

Reference Classes: Accessing methods via [[...]], bug?

Yes, as presented on that site it makes a little more sense:

"While experimenting with the new reference classes in R I noticed some 
odd behaviour if you use the "[[ ]]" notation for methods 
(X[["doSomething"]] instead of X$doSomething). This notation works for 
fields, but I initially thought it wouldn't work for methods until I 
found that if you execute "class(X$doSomething)" you can then use "[[ 
]]" afterwards. The simple example below illustrates the point."

For reference classes, "[[" is not meant to be used either for fields or 
methods.  That it "works" at all is an artifact of the implementation 
using environments.  And arguably the failure to throw an error in that 
circumstance is a bug.

Please use the API as described in the ?ReferenceClasses documentation. 
  These are encapsulated methods, in the usual terminology, with the 
operator "$" playing the role normally assigned to "." in other languages.

A separate  but related issue:  It is possible to define S4 methods for 
reference classes, as discussed in a previous thread, arguably also an 
artifact in that a reference class is implemented as an S4 class of the 
same name.  These are functional methods, associated with a generic 
function, and so outside the encapsulation paradigm.

It would be interesting to get some experience and opinions on whether 
this is a good idea or not.  It breaks encapsulation, in that the 
behavior of the class can no longer be inferred from the class 
definition alone.  On the other hand, it is convenient and relates to 
"operator overloading" in some other languages.

John
On 4/30/11 7:54 PM, Hadley Wickham wrote: