On Fri, Feb 26, 2010 at 11:57 AM, Matt Asher <sta...@quo.org> wrote: > Hi, > > This: > > agents <- list(agent(1), agent(2)) > > worked perfectly. Thanks Uwe! > > I'm not fully sure what you mean by this: > > " > Anyway, I hope you know that lexical scoping will yield in the > environments attached to all those functions they have been generated in > and you know about possible consequences. If not, you really should not > be doing this ... (nor using <<- ) ... > " > > I used the <<- assignment because I am treating these variables in the > sub-function as "belonging" to the parent function (like using "self" or > "this" in other languages). I am basically treating my agent function like a > class that can have instances and class vars, without having to use R's > backwards (IMO) way of doing OOP. >
In that case you might want to look at the proto package http://r-proto.googlecode.com which can do this a bit more cleanly and also allows for delgation (i.e. the counterpart to inheritance in the prototype model) > library(proto) > agent <- proto(id = NA, get_id = function(self) self$id, set_id = > function(self, id) self$id <- id) > > agent$set_id(2) > agent$get_id() [1] 2 ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.