[R] S4: Initialization method called during setClass??

Martin Morgan mtmorgan at fhcrc.org
Sat Jun 6 03:48:46 CEST 2009


Wacek Kusnierczyk wrote:
> Martin Morgan wrote:
> 
> [...]
> 

>>> from a contiguous sequence of integers, starting at 1 (this doesn't seem
>>> to be a particularly extravagant idea either):
>>>
>>>     setClass('foo', representation(id='integer'))
>>>     setMethod('initialize', 'foo', local({
>>>         id=0L
>>>         function(.Object, ...) {
>>>            id <<- id+1L
>>>            .Object at id = id
>>>            return(.Object) } }))
>>>
>>>     foos = replicate(3, new('foo'))
>>>     sapply(foos, slot, 'id')
>>>     # 1 2 3
>>>
>>>     setClass('bar', contains='foo', representation(name='character'))
>>>     setMethod('initialize', 'bar', function(.Object, name='bar', ...) {
>>>        .Object at name = name
>>>        callNextMethod() })
>>>     bars = replicate(3, new('bar'))
>>>     sapply(bars, slot, 'id')
>>>     # 6 7 8
>>>
>>> what??  why on earth creating a subclass *calls* (twice!) the
>>> superclass's initializer?  is there no other way for checking the
>>>     
>> once when the superclass is instantiated (fair enough!) 
> 
> fair?  what's fair here?  that just creating a subclass involves
> instantiation of the superclass?  makes little sense to me.

I completely misread your post, sorry. Martin




More information about the R-help mailing list