[R] inheritence in S4

Martin Morgan mtmorgan at fhcrc.org
Mon Mar 24 19:00:58 CET 2008


cgenolin at u-paris10.fr wrote:
>> callGeneric is an advanced topic.
> 
> Ok, when I will be older :-)
> 
>>> *************************
>>> This works :
>>>
>>> setMethod("initialize","B",
>>>          function(.Object,..., yValue){
>>>              callNextMethod(.Object, ..., y=yValue)
>>>              return(.Object)
>>>          })
>>> new("B",yValue=3)
>>>
>>> but this does not :
>>>
>>> setMethod("initialize","B",
>>>          function(.Object, yValue){
>>>              callNextMethod(.Object, y=yValue)
>>>              return(.Object)
>>>          })
>>> new("B",yValue=3)
>>>
>>> Why ?
>>> Is there any help page about ... ?
>>
>> Both 'work' in the sense that an object is returned
> 
> Well yes, but the second one does return an object without assigning the 
> value 3, that is not realy working...

I wasn't paying enough attention to your code. callNextMethod is like 
any R function -- it will not change .Object 'in place', but makes a 
copy, modifies the copy, and returns the copy. So either

setMethod("initialize","B",
          function(.Object,..., yValue){
              .Object <- callNextMethod(.Object, ..., y=yValue)
              return(.Object)
          })

or more compactly

setMethod("initialize","B",
          function(.Object,..., yValue){
              callNextMethod(.Object, ..., y=yValue)
          })

The code example is incomplete, so I don't really know why one version 
assigned y=3 for you and the other did not; for me, neither version did 
the assignment.

Martin

>> In an object-oriented sense, initialize,B-method should really just 
>> deal with it's own slots; it shouldn't have to 'know' about either 
>> classes that it extends (A) or classes that extend it. And it 
>> shouldn't do work that inherited methods (i.e., initialize,ANY-method) 
>> do.
> 
> I get your point and I agree : I am developing B, you are developing A, 
> I do not want to know what is in A so B should not initialize its 'A part'
> 
> On the other hand, I do not like the "..." . "..." can be anything, 
> there is no controle at all, no type checking.
> I would prefers to initialize B giving its value for its own slot AND an 
> object class A. So I send you the value for A, you send me an objets 
> 'aaa' of class A then I initialize B with some value and aaa. This way, 
> B keep its role but does not transmit anythink to A without controling it.
> 
> Best,
> 
> Christophe
> 
> ----------------------------------------------------------------
> Ce message a ete envoye par IMP, grace a l'Universite Paris 10 Nanterre
> 
> 
> 


-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M2 B169
Phone: (206) 667-2793



More information about the R-help mailing list