[Bioc-devel] When to use generics?

Pages Gallego, M. M@P@ge@G@||ego @end|ng |rom umcutrecht@n|
Mon Apr 29 15:54:13 CEST 2019


Dear all,

I am currently developing a package and I am a bit confused in when one should define a generic. Let me propose an example:
Suppose I have defined 3 classes: one for proteomics data, one for metabolomics data and one for transcriptomics data. I have a function “foo(x)” that will do the same to any of the 3 classes, but requires a slightly different implementation for each one. I could do something like that:

```
foo <- function(x) {
    if (is(x, ‘proteomics’)) {
          foo.protein(x)
    } else if (is(x, ‘metabolomics’)) {
          foo.metabo(x)
    } else if (is(x, ‘transcriptomics’)) {
          foo.trans(x)
    }
}
```

And then define foo.protein, foo.metabo and foo.trans.
But this is basically the same as defining the generic “foo” and then defining a method for each of the classes.

The problem is that “foo” is not generic “enough” in terms that outside the package it has no use. Therefore, defining the generic seems like a big responsibility in that it should have a use outside the package. Would you still define “foo” as generic in this case?  Are there any guidelines in when one should or should not define a generic?

From: https://www.bioconductor.org/help/course-materials/2017/Zurich/S4-classes-and-methods.html#extending-a-class-without-adding-any-slot
in section 2.2.2 it recommends to reuse existing generics (which makes a lot of sense), but then it also says that you can define new generics for specialized operations, what if the operation is very specialized?
From: https://www.bioconductor.org/help/course-materials/2011/AdvancedRFeb2011Seattle/ImplementingS4Objects-lab.pdf
in section 2.3 it says that the recommended way to implement accessors is through methods (which require generics if none exist), is this always the case for all S4 classes?
I apologize in advance if these are very naïve questions.

Best,
Marc


------------------------------------------------------------------------------

De informatie opgenomen in dit bericht kan vertrouwelijk zijn en is
uitsluitend bestemd voor de geadresseerde. Indien u dit bericht onterecht
ontvangt, wordt u verzocht de inhoud niet te gebruiken en de afzender direct
te informeren door het bericht te retourneren. Het Universitair Medisch
Centrum Utrecht is een publiekrechtelijke rechtspersoon in de zin van de W.H.W.
(Wet Hoger Onderwijs en Wetenschappelijk Onderzoek) en staat geregistreerd bij
de Kamer van Koophandel voor Midden-Nederland onder nr. 30244197.

Denk s.v.p aan het milieu voor u deze e-mail afdrukt.

------------------------------------------------------------------------------

This message may contain confidential information and is intended exclusively
for the addressee. If you receive this message unintentionally, please do not
use the contents but notify the sender immediately by return e-mail. University
Medical Center Utrecht is a legal person by public law and is registered at
the Chamber of Commerce for Midden-Nederland under no. 30244197.

Please consider the environment before printing this e-mail.

	[[alternative HTML version deleted]]



More information about the Bioc-devel mailing list