Janko Thyson
2011-May-27 13:13 UTC
[Rd] Reference Classes/S4 Classes: can method dispatch check superclasses BEFORE resorting to method for "ANY"?
Dear list, is it possible that method dispatch checks for superclasses/virtual classes before checking "ANY"? I'd like to build a generic initialization method for all my Reference Class (say "MyDataFrame") objects by having them inherit from class, say "MyRefClassVirtual" (which would have to be a virtual S4 class; there are no virtual Reference Classes, are there?) The problem is that 'getRefClass("MyDataFrame")$new' calls (I think) the method that was written for "ANY". Thus even though I write a explicit initialize method for class "MyRefClassVirtual" which I should be called for "MyDataFrame" as it inherits from this class, this method will never be called because "ANY beats anything else". So, I think I'd like to tell the method somehow to check for superclass/virtual classes *before* resorting to "ANY". Is that possible? Regards, Janko
Martin Morgan
2011-May-27 13:22 UTC
[Rd] Reference Classes/S4 Classes: can method dispatch check superclasses BEFORE resorting to method for "ANY"?
On 05/27/2011 06:13 AM, Janko Thyson wrote:> Dear list, > > is it possible that method dispatch checks for superclasses/virtual > classes before checking "ANY"? > > I'd like to build a generic initialization method for all my Reference > Class (say "MyDataFrame") objects by having them inherit from class, say > "MyRefClassVirtual" (which would have to be a virtual S4 class; there > are no virtual Reference Classes, are there?)Reference classes can be virtual; 'initialize' is a reference method, not an S4 method. .A <- setRefClass("A", contains="VIRTUAL", methods=list( initialize=function(..., msg="initialize,AA") { callSuper(...) message(msg) .self })) .AA <- setRefClass("AA", contains="A") > .A$new() Error in methods::new(def, ...) : trying to generate an object from a virtual class ("A") > .AA$new() initialize,AA An object of class "AA" <environment: 0x1b82f48> Martin> The problem is that 'getRefClass("MyDataFrame")$new' calls (I think) the > method that was written for "ANY". Thus even though I write a explicit > initialize method for class "MyRefClassVirtual" which I should be called > for "MyDataFrame" as it inherits from this class, this method will never > be called because "ANY beats anything else". > > So, I think I'd like to tell the method somehow to check for > superclass/virtual classes *before* resorting to "ANY". > > Is that possible? > > Regards, > Janko > > ______________________________________________ > R-devel at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-devel-- Computational Biology Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: M1-B861 Telephone: 206 667-2793
Seemingly Similar Threads
- Reference classes: accessor functions via 'getRefClass(...)$accessors(...)'
- Ref Classes: bug with using '.self' within initialize methods?
- Reference Classes: removing methods -> implications for objects/instances of that class
- possibly invalid assertion in setRefClass?
- How can I write methods for 'as()'?