Re: [R] inheritence in S4

From: Christophe Genolini <cgenolin_at_u-paris10.fr>
Date: Thu, 06 Mar 2008 00:40:23 +0100

Hi Martin

I was not that much speaking about what we can do, but more about what we can't. We can't decide that object will be 'never empty', we have to allow empty oject, otherwise new("A") will not work and that will be problematic.

So, at this point, I see :

In consequence, to program as safe as possible:

Here is an example (still the same stupid example with a single slot. Obviously, it has no interest on so small object, it is just to simplify)

setClass("A", representation(x="numeric")) setMethod("checkCoherance","A",

    function(object){

        cat("*** checkCoherance A ***\n")
        if(length(object_at_x)>0){
            if(object_at_x<0){stop("Object incoherance : x should be 
positive")}else{}
        }else{}
        return(TRUE)

    }
)

setMethod("initialize","A",

    function(.Object,value=numeric(0)){

        cat("*** initialize A ***\n")
        .Object_at_x <- value
        checkCoherance(.Object)
        return(.Object)

    }
)

setGeneric("setX<-",function(object,value){standardGeneric("setX<-")}) setReplaceMethod("setX","A",
  function(object,value){
    object_at_x <- value
    checkCoherance(object)
    return(object)
  }
)
 

a1 <- new("A")
a2 <- new("A",value=4)
try(a3 <- new("A",value=-4))

A <- function(val=numeric(0)){
  new("A",value=val)
}

A()
A(val=4)
A(val=-4)

setClass("C", representation(y="numeric"),contains="A") setMethod("checkCoherance","C",

    function(object){

        cat("*** checkCoherance C ***\n")
        if(length(object_at_y)>0){
            if(object_at_y>0){stop("Object incoherance : y should be 
négative")}else{}
        }else{}
        checkCoherance(as(object,"A"))
        return(TRUE)

    }
)

setMethod("initialize","C",

    function(.Object,valueX=numeric(0), valueY=numeric(0)){

        cat("*** initialize C ***\n")
        .Object_at_x <- valueX
        .Object_at_y <- valueY
        checkCoherance(.Object)
        return(.Object)

    }
)

c <- new("C",valueX=10,valueY=12)
new("C")

C <- function(valX=numeric(0),valY=numeric(0)){

   new("C",valueX=valX,valueY=valY)
}
C()
C(valX=5)
try(C(valX=-5))
C(valY=-3)
c1 <- C(valX=4,valY=-6)
as(c1,"A")

setClass("A", representation(x="numeric"),

    prototype(x=0)
)

> If you have an initialize
> method on "A" then it will be called. Only if you call the A constructor, right ? Not if you set the slot that C has heritate from A one by one, right ?

Christophe



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. Received on Wed 05 Mar 2008 - 23:54:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 06 Mar 2008 - 00:30:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive