Re: [R] Re: S4 method inheritance

From: Robert Gentleman <>
Date: Tue 24 May 2005 - 23:27:56 EST

Duncan Murdoch wrote:
> Ross Boylan wrote:
>> On Mon, 2005-05-23 at 14:41 -0700, Ross Boylan wrote:
>> ....
>>> Finally, I'm a bit concerned that one article mentioned that S4
>>> inheritance, in practice, is used mostly for data, not methods (Thomas
>>> Lumley, R News 4(1), June 2004: p. 36). Am I going down a road I
>>> shouldn't travel?
>> Hmm, maybe I just found out. If B is an S4 subclass of A (aka extends
>> A), how does B's method foo invoke A's foo?
> Your question doesn't make sense in S4. In S4, classes don't have
> methods, generics have methods. There's no such thing as "B's method"
> or "A's method".
> You might get what you want with foo(as(bObject, "A")) if bObject is an
> instance of class B.
>> The question assumes that A's foo was defined as an in place function,
>> so there's no (obvious) named object for it, i.e,
>> setMethod("A", signature(blah="numeric"), function(x) something)


In general it may be best to think of a generic function as a dispatching mechanism. For S4 methods are associated with a specific generic function. A generic knows about all methods that are associated with it, and about no others. Thus in S4, the little tiff over who owns label goes away - they both do - different packages can define generic functions for label, or anything else they care to, and users can write methods for specific generic functions and associate them with a generic. [Note that in the S3 system there is no real mechanism for saying that is a method for one generic named foo, and not for another - but the language does allow for multiple generics named foo - one of the very many reasons that S3 does not really do what you want it to, but many seem convinced otherwise].

The class hierarchy of the actual supplied arguments, is used to determine the dispatch order (a linearization of the available methods) once the generic is invoked. The most specific method is used first. It may intiate a call to callNextMethod (S4) or NextMethod (S3) to transfer control to the next most specific method - the manual pages provide more specific details.

As Duncan said - classes do not own methods in this paradigm. Generic functions do.


> I don't know what you mean by "in place function", but I hope my answer
> helps anyway.
> Duncan Murdoch
> ______________________________________________
> mailing list
> PLEASE do read the posting guide!
> mailing list PLEASE do read the posting guide! Received on Tue May 24 23:33:34 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:01 EST