Janko Thyson
2013-Mar-20 16:38 UTC
[R] S4 Reference Classes: undesired behavior when calling method '$field()'
Dear list, I came across a behavior that IMHO is somewhat undesired when calling '$field()': If the field name whose value you're trying to get is *not* a valid field of the Reference Class, then R doesn't stop there with an error, but scans through all enclosing environments/frames. The result is something similar to calling '|get(<objname>, inherits=TRUE)|' when you'd actually expect something like ' get(<objname>, inherits=FALSE)' to be the default (at least I do) in order to avoid undesired retrieval results. Here's a little illustration of the behavior: http://stackoverflow.com/questions/15529251/avoiding-consideration-of-enclosing-frames-when-retrieving-field-value-of-a-s4-r Best regards, Janko [[alternative HTML version deleted]]
Gabor Grothendieck
2013-Mar-20 16:53 UTC
[R] S4 Reference Classes: undesired behavior when calling method '$field()'
On Wed, Mar 20, 2013 at 12:38 PM, Janko Thyson <janko.thyson.rstuff at googlemail.com> wrote:> Dear list, > > I came across a behavior that IMHO is somewhat undesired when calling > '$field()': > If the field name whose value you're trying to get is *not* a valid > field of the Reference Class, then R doesn't stop there with an error, > but scans through all enclosing environments/frames. The result is > something similar to calling '|get(<objname>, inherits=TRUE)|' when > you'd actually expect something like ' get(<objname>, inherits=FALSE)' > to be the default (at least I do) in order to avoid undesired retrieval > results. > > Here's a little illustration of the behavior: > http://stackoverflow.com/questions/15529251/avoiding-consideration-of-enclosing-frames-when-retrieving-field-value-of-a-s4-rNote that that behavior is documented in ?setRefClass : $field(name, value) With one argument, returns the field of the object with character string name. With two arguments, the corresponding field is assigned value. Assignment checks that name specifies a valid field, ** but the single-argument version will attempt to get anything of that name from the object's environment. ** -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Apparently Analagous Threads
- Bug or feature: using "ANY" as a generic field class (was: '[R] Is there a (virtual) class that all R objects inherit from?)
- reference classes: question on inheritance
- WG: Reference classes: error with missing arguments in method calls
- Reference Classes/S4 Classes: can method dispatch check superclasses BEFORE resorting to method for "ANY"?
- Reference classes: accessor functions via 'getRefClass(...)$accessors(...)'