From mtmorgan at fredhutch.org  Thu May 28 16:30:13 2015
From: mtmorgan at fredhutch.org (Martin Morgan)
Date: Thu, 28 May 2015 07:30:13 -0700
Subject: [Rd] S4 inheritance and old class
In-Reply-To: <CAMDmGSbB+0wOfCCfnpBY4ENDkrKozb0iaUFW10sXprDp_7WTEw@mail.gmail.com>
References: <CAMDmGSbB+0wOfCCfnpBY4ENDkrKozb0iaUFW10sXprDp_7WTEw@mail.gmail.com>
Message-ID: <55672675.7020104@fredhutch.org>

On 05/28/2015 02:49 AM, Julien Id? wrote:
> Hey everyone,
>
> I would like to develop a package using S4 classes.
> I have to define several S4 classes that inherits from each others as
> follow:
>
> # A <- B <- C <- D
>
> I also would like to define .DollarNames methods for these class so, if I
> have understood well, I also have to define an old class as follow:
>
> # AOld <- A <- B <- C <- D
>
> setOldClass(Classes = "AOld")
>
> setClass(
>    Class = "A",
>    contains = "AOld",
>    slots = list(A = "character")
> )
>
> .DollarNames.A <- function(x, pattern)
>    grep(pattern, slotNames(x), value = TRUE)

Instead of setOldClass, define a $ method on A

     setMethod("$", "A", function(x, name) slot(x, name))

And then

   a = new("A")
   a$<tab>
   d = new("D")
   d$<tab>

I don't know about the setOldClass problem; it seems like a bug.

Martin Morgan

>
> setClass(
>    Class = "B",
>    contains = "A",
>    slots = list(B = "character"),
>    validity = function(object){
>      cat("Testing an object of class '", class(object),
>          "'' with valitity function of class 'B'", sep = "")
>      cat("Validity test for class 'B': ", object at A, sep = "")
>      return(TRUE)
>    }
> )
>
> setClass(
>    Class = "C",
>    contains = c("B"),
>    slots = list(C = "character"),
>    validity = function(object){
>      cat("Testing an object of class '", class(object),
>          "'' with valitity function of class 'C'", sep = "")
>      cat("Validity test for class 'C': ", object at A, sep = "")
>      return(TRUE)
>    }
> )
>
> setClass(
>    Class = "D",
>    contains = "C",
>    slots = list(D = "character"),
>    validity = function(object){
>      cat("Testing an object of class '", class(object),
>          "'' with valitity function of class 'D'", sep = "")
>      cat("Validity test for class 'D': ", object at A, sep = "")
>      return(TRUE)
>    }
> )
>
> My problem is that when I try to create an object of class "D" and test its
> validity
>
> validObject(new("D"))
>
> it seems that at some point the object is coerced to an object of class
> "AOld" and tested by the validity function of class "B". What am I missing
> here?
>
> Julien
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>


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

Location: Arnold Building M1 B861
Phone: (206) 667-2793



[Rd] S4 inheritance and old class
Martin Morgan