Daniel Lee
2010-Oct-28 14:13 UTC
[Rd] Reference Classes: Generalizing Reference Class Generator objects?
Is it possible to override the $new(...) in the reference class generator? I have tried adding a "new" method to the methods of the class, but that is obviously not correct. I have also tried adding it to the class generator, but the class generator still uses the default constructor. As a simple example, this is the current interface: TestClass <- setRefClass ("TestClass", fields = list (text = "character"), methods = list ( print = function () {cat(text)}) ) test <- TestClass$new (text="Hello World") test$print() I would like to override $new(...) to be something like (add a "\n" to the end of the input, no need to specify input fields): TestClass$methods (new = function (text) { text <- paste (text, "\n") methods:::new (def, text=text) }) The constructor would then be: test <- TestClass$new ("Hello World") This is a subtle, but useful change. I have also tried adding to TestClass a method $newInstance(text), but that was not successful. If this is not possible, could we consider augmenting the Reference Class interface to include constructors?
Jon Clayden
2010-Oct-28 16:07 UTC
[Rd] Reference Classes: Generalizing Reference Class Generator objects?
Hi Daniel, I think you want to define an "initialize" method, as in TestClass <- setRefClass ("TestClass", fields = list (text = "character"), methods = list ( initialize = function (text) { object <- initFields(text=paste(text,"\n")) }, print = function () { cat(text) } ) ) This seems to work as you intend:> x <- TestClass$new("test") > x$print()test All the best, Jon On 28 October 2010 15:13, Daniel Lee <bearlee at alum.mit.edu> wrote:> Is it possible to override the $new(...) in the reference class generator? I > have tried adding a "new" method to the methods of the class, but that is > obviously not correct. I have also tried adding it to the class generator, > but the class generator still uses the default constructor. > > As a simple example, this is the current interface: > TestClass <- setRefClass ("TestClass", > ? ? ? ?fields = list (text = "character"), > ? ? ? ?methods = list ( > ? ? ? ? ? ? ? ?print = function () ?{cat(text)}) > ) > test <- TestClass$new (text="Hello World") > test$print() > > I would like to override $new(...) to be something like (add a "\n" to the > end of the input, no need to specify input fields): > TestClass$methods (new = function (text) { > ? ? ? ? ? ?text <- paste (text, "\n") > ? ? ? ? ? ?methods:::new (def, text=text) > ? ? ? ?}) > > The constructor would then be: > test <- TestClass$new ("Hello World") > > This is a subtle, but useful change. I have also tried adding to TestClass a > method $newInstance(text), but that was not successful. If this is not > possible, could we consider augmenting the Reference Class interface to > include constructors? > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
Jon Clayden
2010-Oct-28 16:12 UTC
[Rd] Reference Classes: Generalizing Reference Class Generator objects?
Sorry - you don't need to assign the value of initFields(). I was going to do it in two lines but then realised one was enough... :) TestClass <- setRefClass ("TestClass", fields = list (text = "character"), methods = list ( initialize = function (text) { initFields(text=paste(text,"\n")) }, print = function () { cat(text) } ) ) All the best, Jon On 28 October 2010 15:13, Daniel Lee <bearlee at alum.mit.edu> wrote:> Is it possible to override the $new(...) in the reference class generator? I > have tried adding a "new" method to the methods of the class, but that is > obviously not correct. I have also tried adding it to the class generator, > but the class generator still uses the default constructor. > > As a simple example, this is the current interface: > TestClass <- setRefClass ("TestClass", > ? ? ? ?fields = list (text = "character"), > ? ? ? ?methods = list ( > ? ? ? ? ? ? ? ?print = function () ?{cat(text)}) > ) > test <- TestClass$new (text="Hello World") > test$print() > > I would like to override $new(...) to be something like (add a "\n" to the > end of the input, no need to specify input fields): > TestClass$methods (new = function (text) { > ? ? ? ? ? ?text <- paste (text, "\n") > ? ? ? ? ? ?methods:::new (def, text=text) > ? ? ? ?}) > > The constructor would then be: > test <- TestClass$new ("Hello World") > > This is a subtle, but useful change. I have also tried adding to TestClass a > method $newInstance(text), but that was not successful. If this is not > possible, could we consider augmenting the Reference Class interface to > include constructors? > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
Maybe Matching Threads
- Creating a reference class object from a class definition in a package fails
- Reference Class error message: may be caused by lazy evaluation?
- General "nil" reference class object
- Internally accessing ref class methods with .self$x is different from .self[['x']]
- Question about copying reference objects using the initialize method