Hi! I'm trying to learn about object oriented R, as it seems like it would be very useful.
I'm going over an example from the documentation, and I'm very confused: http://www1.maths.lth.se/help/R/R.oo/ [assume you've called library (R.oo)] setConstructorS3("SavingsAccount", function(balance=0) { if (!is.numeric(balance) && length(balance) != 1) throw("Argument 'balance' must be a single numeric value: ", mode(balance)); if (balance < 0) throw("Trying to create an account with a negative balance."); extend(Object(), "SavingsAccount", .balance = balance ); }) Why would I get the following result? > SavingsAccount(5) [1] "SavingsAccount: 0x18706032" > SavingsAccount("fruit") [1] "SavingsAccount: 0x605387128" The second time, the input should fail, balance is not numeric. ------------------------------ A second attempt, with my own code: setConstructorS3("StreamingFileReader", function(fileName, hasHeader=T, sep="\t") { if (missing (fileName)) throw ("Must supply a file name!"); if (missing(hasHeader)) hasHeader <-T; if (missing(sep)) sep="\t"; extend(Object(), "StreamingFileReader", .fileName=fileName, .hasHeader=hasHeader, .sep=sep); }) > z= StreamingFileReader("temp") Error in list(`StreamingFileReader("temp")` = <environment>, `extend(Object(), "StreamingFileReader", .fileName = fileName, .hasHeader = hasHeader, .sep = sep)` = <environment>, : [2009-07-17 17:37:15] Exception: Must supply a file name! at throw(Exception(...)) at throw.default("Must supply a file name!") at throw("Must supply a file name!") at constructor() at getStaticInstance.Class(class) at getStaticInstance(class) at getStaticInstance.Object(this) at getStaticInstance(this) at extend.Object(Object(), "StreamingFileReader", .fileName = fileName, .hasHeader = hasHeader, .sep = at extend(Object(), "StreamingFileReader", .fileName = fileName, .hasHeader = hasHeader, .sep = sep) at StreamingFileReader("temp") > z= StreamingFileReader("temp") > z$.fileName [1] "temp" Why is it that the first time I try to construct the object it fails, and the second time it seems to work just fine? Is there something I should be doing between declaring the constructor and using the object (besides adding S3 methods?) Thanks for any help, I find this entirely confusing. -Jim Nemesh [[alternative HTML version deleted]] ______________________________________________ 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.