Dear UseRs, A simple class inheritance example:> setClass("test",representation(a="numeric")) > setMethod("initialize","test",function(.Object,x,...){ print("Initialization!!!") callNextMethod(.Object,a=x,...) })> new("test",x=23)[1] "Initialization!!!" An object of class "test" Slot "a": [1] 23> setClass("test2",contains="test",representation(b="integer"))[1] "Initialization!!!" Error in .nextMethod(.Object, a = x, ...) : argument "x" is missing, with no default When trying to define a new class "test2" above, "initialize" method for "test" is called (??? looks like a bug to me) and naturally argument "x" is not found. Thus I can not create a subclass for "test". I could not find anything in documentation. Thanks, Vitalie.
Martin Morgan
2009-Jun-05 16:49 UTC
[R] S4: Initialization method called during setClass??
Hi Vitalie -- Vitalie S. wrote:> Dear UseRs, > > A simple class inheritance example: > >> setClass("test",representation(a="numeric")) >> setMethod("initialize","test", > function(.Object,x,...){ > print("Initialization!!!") > callNextMethod(.Object,a=x,...) > }) > >> new("test",x=23) > [1] "Initialization!!!" > An object of class "test" > Slot "a": > [1] 23the implicit contract is that the initialize method is written so that new("test") works. This contract comes up at several points during class definition and instantiation. You've identified one of those points.> >> setClass("test2",contains="test",representation(b="integer")) > [1] "Initialization!!!" > Error in .nextMethod(.Object, a = x, ...) : argument "x" is missing, > with no defaultI'm not sure of the details, but part of the class definition is a coercion method, and in defining this new() is called, without arguments, on the contained classes. Martin> > > When trying to define a new class "test2" above, "initialize" method for > "test" is called (??? looks like a bug to me) and naturally argument "x" > is not found. Thus I can not create a subclass for "test". > I could not find anything in documentation. > Thanks, > Vitalie. > > ______________________________________________ > R-help at 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.