[Bioc-devel] S4 overwrite inspector of virtual class

Gabe Becker becker.gabe at gene.com
Mon Aug 15 19:32:26 CEST 2016


Ah, yeah we usually call them validator functions or (more commonly)
validity functions/validity method.

The issue with what you want is that if A is a superclass of B (B inherits
from  or "contains" A) then objects of class B must be valid objects of
class A. From ?validObject (emphasis mine)

 Validity testing takes place 'bottom up': Optionally, if
     'complete=TRUE', the validity of the object's slots, if any, is
     tested.  Then, *in all cases, for each of the classes that this*
*     class extends (the 'superclasses'), the explicit validity method*
*     of that class is called, if one exists.*  Finally, the validity
     method of 'object''s class is called, if there is one.

So the system is specifically designed not to allow what you're asking, as
it violates one of the tenets of R's version of OOP. The virtual class
should contain only slots, validity checks, etc that apply to literally all
subclasses.

There are ways to get around this, such as if a slot indicates what version
it is in your chromosome example, the validity check in the virtual class
could behave differently based on the value of that slot. This blurs the
line between differentiating behavior on objects by class or by contents,
though, so should (I think) be used sparingly and with care. Generally you
want to use method dispatch for class-specific differences in behavior.

Basically if a check should be different for different subclasses, it
shouldn't go in the validity method for the virtual, it should be explicit
in the the validity methods for each subclass. Keep in mind you can assign
an existing R closure (function) there, so you don't have to repeat code,
just do

.lcaseCheck = function(object) {blabla}
.ucaseCheck = function(object) {BlaBla}


and use .lcaseCheck and .ucaseCheck either as the validity functions for
your classes if they that is the only check:

setClass("CoolThing", ..., validity=.lcaseCheck)
setClass("EvenCoolerThing", ..., validity = .lcaseCheck)
setClass("DifferentCoolThing", ..., validity = .ucaseCheck)


or within them as necessary. You can dispatch on the class within a
validity method from what I can see, but that is probably overkill for a
case like this.


HTH,
~G

On Mon, Aug 15, 2016 at 9:56 AM, Zach Skidmore <zskidmor at wustl.edu> wrote:

> maybe validator is the proper term in R? i've highlighted what I refer to
> as the inspector below as an example:
>
> setClass("file",
>                 contains="file_virtual",
>                 validity=function(object){
>                 # Check that object is as expected
>
>                 }
> )
>
> On 8/15/16 11:46 AM, Gabe Becker wrote:
>
> Zach,
>
> Is an inspector a method you define on your classes? I'm not quite
> following what you mean by your question. AFAIK inspectors are not
> generally a thing in R (at least that go by that name).
>
> ~G
>
> On Mon, Aug 15, 2016 at 9:42 AM, Zach Skidmore <zskidmor at wustl.edu> <zskidmor at wustl.edu> wrote:
>
>
> Hi All,
>
> I'm currently transforming the GenVisR package into an Object Oriented
> system. Currently I have a virtual class and several child-classes. I am
> wondering if there is a way to tell R to use the inspector of the virtual
> class only if the inspector of a child class in not defined.
>
> For example say I had a class to store versions of a file-type and I have
> a slot in the class to store the position. Between different versions of
> the file-type there may be small differences (for example Chromosome may be
> capitalized in version 2,3,4 but not version 1). Ideally the child classes
> for 2,3,4 would be able to inherit the inspector from the virtual class to
> check the chromosome name and I would define a separate inspector for
> version 1 which is different.
>
> Any thoughts? Currently both inspectors are called (virtual and the
> appropriate sub-class), meaning if i added more versions in the future I
> would have to re-write the virtual and child class. Whereas if I could say
> ignore the virtual (i.e. default) inspector if another is defined I would
> only have to write the child class inspector in the future.
>
> Hopefully this makes sense, let me know if it doesn't or if i'm violating
> a core OO principle, i'm relatively new to object oriented programming.
>
> Thanks, Zach!
>
> ________________________________
> The materials in this message are private and may contain Protected
> Healthcare Information or other information of a sensitive nature. If you
> are not the intended recipient, be advised that any unauthorized use,
> disclosure, copying or the taking of any action in reliance on the contents
> of this information is strictly prohibited. If you have received this email
> in error, please immediately notify the sender via telephone or return mail.
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________Bioc-devel at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/bioc-devel
>
>
>
> ------------------------------
>
> The materials in this message are private and may contain Protected
> Healthcare Information or other information of a sensitive nature. If you
> are not the intended recipient, be advised that any unauthorized use,
> disclosure, copying or the taking of any action in reliance on the contents
> of this information is strictly prohibited. If you have received this email
> in error, please immediately notify the sender via telephone or return mail.
>



-- 
Gabriel Becker, Ph.D
Associate Scientist
Bioinformatics and Computational Biology
Genentech Research

	[[alternative HTML version deleted]]



More information about the Bioc-devel mailing list