From jmc at r-project.org  Fri Oct  6 15:48:49 2006
From: jmc at r-project.org (John Chambers)
Date: Fri, 06 Oct 2006 09:48:49 -0400
Subject: [Rd] class unions?
In-Reply-To: <EBA89392-7277-48CA-9AF8-F134D860C710@mac.com>
References: <EBA89392-7277-48CA-9AF8-F134D860C710@mac.com>
Message-ID: <45265EC1.5040004@r-project.org>

The problem is a combination of a class union definition that includes a 
basic type as a member, and is not in a package.

Class "numeric" is sealed, so at present setClassUnion() doesn't cache 
the new superclass in the definition of "numeric".  In 2.4.0 that 
happens when a package is attached (before, it didn't happen at all).  
It's _probably_ true that the caching should take place in 
setClassUnion(), but that needs a little consideration.

Meanwhile the workaround is to call cacheMetaData(1) to cache the 
information in the global environment--that's what happens when a 
corresponding package is attached.

 > setClassUnion("index", c("numeric", "character", "logical"))
[1] "index"
 > cacheMetaData(1)
 > extends("numeric")
[1] "numeric"   "vector"    "index"


Parlamis Franklin wrote:
> the code below has me confused:
>
> 	setClassUnion("index", c("numeric", "character", "logical"))
> 	extends("numeric") # i don't see the class union
> 	library(Matrix)
> 	extends("numeric") # now i see the class union
>
> i am aware that the "Matrix" package separately defines the "index"  
> class union.
> is it the case that class definitions need to occur in packages?
>
> i am also having trouble dispatching on class unions defined in the  
> global environment,
> which may just be the same problem.
Yes.  You need to cacheMetaData(1) before calling foo(2).
>  see code below:
>
> 	setClassUnion("numORchar", c("numeric", "character"))
> 	foo <- function(x) "Default"
> 	setMethod("foo", signature(x = "numORchar"),
> 		function(x) "numORchar")
> 	foo(2) # gives the default method
>
> franklin
>
>  > version
>                 _
> platform       powerpc-apple-darwin8.7.0
> arch           powerpc
> os             darwin8.7.0
> system         powerpc, darwin8.7.0
> status
> major          2
> minor          4.0
> year           2006
> month          10
> day            03
> svn rev        39566
> language       R
> version.string R version 2.4.0 (2006-10-03)
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>




[Rd] class unions?
John Chambers