Hi, I am working on a project, which contains S4 classes and subclasses. Lets assume the following organisation: A: S4 Class B,C: inherit from A D,E,F,G: inherit from B H,I: inherit from C I want to define now a generic function, which returns me the name of the class. I can now write the function with "A" in the signature. Is there any reason to write the function for B,C, D,E,... with "B","C","D","E", .. in the signature ? I can imagine that it can become a mess if for example I write a function for "A" and a special function for "I" (lets assume, I is the only exception). Have you any best-work-pratice to share ? Thanks Alexander -- View this message in context: http://r.789695.n4.nabble.com/Definition-of-generic-function-for-subclasses-tp4468837p4468837.html Sent from the R help mailing list archive at Nabble.com.
On 03/13/2012 04:34 AM, Alexander wrote:> Hi, > I am working on a project, which contains S4 classes and subclasses. Lets > assume the following organisation: > A: S4 Class > B,C: inherit from A > D,E,F,G: inherit from B > H,I: inherit from C > > I want to define now a generic function, which returns me the name of the > class. I can now write the function with "A" in the signature. Is there any?class for this function> reason to write the function for B,C, D,E,... with "B","C","D","E", .. in > the signature ? I can imagine that it can become a mess if for example IImplement the method at the lowest (highest?) level in the hierarchy as possible -- so on "A" only for the example of class.> write a function for "A" and a special function for "I" (lets assume, I is > the only exception).If 'I' is somehow special then write a method for it alone setClass("A"); setClass("C", "A"); setClass("I", "C") setGeneric("cls", function(x, ...) standardGeneric("cls")) setMethod(cls, "A", function(x, ...) as.vector(class(x))) setMethod(cls, "I", function(x, ...) tolower(as.vector(class(x)))) and then > cls(new("C")) [1] "C" > cls(new("I")) [1] "i"> Have you any best-work-pratice to share ?Keep your class hierarchy simple and relevant to your actual problem. Martin> > Thanks > Alexander > > -- > View this message in context: http://r.789695.n4.nabble.com/Definition-of-generic-function-for-subclasses-tp4468837p4468837.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.-- Computational Biology Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: M1-B861 Telephone: 206 667-2793
The definition of simple in "Keep your class hierarchy simple and relevant to your actual problem" is quite difficult. For someone who has programmed the classes etc, it is quite simple to understand the heritance. But for example for someone else, who has to maintain the code and the classes, it might not. I know how to define classes, and subclasses and generic function (and I know also the helpfunction "?"). It is more about the concept. What is the best way to write good code which is easy to understand by others, easy to maintain, but doesn't repeat the same generic function for every single subclass... -- View this message in context: http://r.789695.n4.nabble.com/Definition-of-generic-function-for-subclasses-tp4468837p4469145.html Sent from the R help mailing list archive at Nabble.com.
Apparently Analagous Threads
- How to get the definition of a class?
- Query super- and subclasses of a class: is there a better way than to use 'completeClassDefinition()'
- new.env does not recognize parents from subclasses of "environment"
- non linear equation
- Why does Object.subclasses_of ignore subclasses within modules?