From osklyar at maninvestments.com  Thu Feb 12 10:22:29 2009
From: osklyar at maninvestments.com (Sklyar, Oleg (London))
Date: Thu, 12 Feb 2009 09:22:29 -0000
Subject: [Rd] setClassUnion with numeric; extending class union
In-Reply-To: <49933796.60809@r-project.org>
References: <1A68FCB28DE72F4BA3B967E6506CCE43047DF65F@mildnpexmb01.maninvestments.ad.man.com>
	<49932B2E.5090703@r-project.org> <49933796.60809@r-project.org>
Message-ID: <1A68FCB28DE72F4BA3B967E6506CCE43047DF669@mildnpexmb01.maninvestments.ad.man.com>

Hi John,

sorry for not posting more info. Strangely I get warnings about
setClassUnion with numeric in a very special case: if I define it in a
clean R session then there are no warnings, however if I load a number
of my packages where there are other classes derived from numeric and
exported then I get the following warnings:

> setClassUnion("numericOrNULL", c("numeric","NULL"))
[1] "numericOrNULL"
Warning messages:
1: In .checkSubclasses(class1, classDef, class2, classDef2, where1,  :
  Subclass "TimeDateBase" of class "numeric" is not local and cannot be
updated for new inheritance information; consider setClassUnion()
2: In .checkSubclasses(class1, classDef, class2, classDef2, where1,  :
  Subclass "TimeDate" of class "numeric" is not local and cannot be
updated for new inheritance information; consider setClassUnion()
3: In .checkSubclasses(class1, classDef, class2, classDef2, where1,  :
  Subclass "Time" of class "numeric" is not local and cannot be updated
for new inheritance information; consider setClassUnion()

The class is operational even with those warnings though. Now, the above
classes are defined as follows:

## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - 
setClass("TimeDateBase", 
    representation("numeric", mode="character"),
    prototype(mode="posix")
)

## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - 
setClass("TimeDate",
    representation("TimeDateBase", tzone="character"),
    prototype(tzone="Europe/London")
)

## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - 
setClass("Time", 
    representation("TimeDateBase")
)

Theses classes work perfectly fine on their own and are used throughout
our code for all possible time and date operations extending the
existing functionality of R and available third party packages by an
order of magnitude. I do not see a relation between the above class
definitions and the newly defined class union though apart from the fact
that they are in a package namespace and therefore locked. Sorry I
cannot provide more source code as the code is not yet public.

It would definitely be nice to somehow have a .Data slot in NULL or even
a data.frame, although I do understand that this is quite a substantial
piece of work to make it all robust and backward compatible.

> sessionInfo() ## of a clean session

R version 2.9.0 Under development (unstable) (2009-02-02 r47821) 
x86_64-unknown-linux-gnu 

locale:
C

attached base packages:
[1] stats     graphics  utils     datasets  grDevices methods   base


Any thoughts are greatly appreciated.

Kind regards,
Oleg

Dr Oleg Sklyar
Research Technologist
AHL / Man Investments Ltd
+44 (0)20 7144 3107
osklyar at maninvestments.com 

> -----Original Message-----
> From: John Chambers [mailto:jmc at r-project.org] 
> Sent: 11 February 2009 20:40
> To: Sklyar, Oleg (London)
> Cc: r-devel at r-project.org
> Subject: Re: [Rd] setClassUnion with numeric; extending class union
> 
> So, I was intrigued and played around a bit more.  Still 
> can't get any 
> warnings, but the following may be the issue.
> 
> One thing NOT currently possible is to have a class that has 
> NULL as its 
> data part, because type NULL is abnormal and can't have attributes.
> 
> So if you want a class that contains a union including NULL, 
> you're in 
> trouble generating a value from the class that is NULL.  It's 
> not really 
> a consequence of the setUnion() per se.
> 
>  > setClass("bar", contains = "numericOrNULL")
> [1] "bar"
>  > zz = new("bar", NULL)
> Error in validObject(.Object) :
>   invalid class "bar" object: invalid object for slot ".Data" 
> in class 
> "bar": got class "list", should be or extend class "numericOrNULL"
> 
> (How one got from the error to the message is a question, but in any 
> case this can't currently work.)
> 
> As in my example and in your example with a slot called "data", no 
> problem in having a slot value that is NULL.
> 
> Looking ahead, I'm working on some extensions that would 
> allow classes 
> to contain "abnormal" data types (externalptr, environment, ...) by 
> using a reserved slot name, since one can not make the actual 
> data type 
> one of those types.
> 
> John Chambers wrote:
> > What warnings? Which part of the following is not what 
> you're looking 
> > for? (The usual information is needed, like version of R, 
> reproducible 
> > example, etc.)
> >
> >
> > > setClassUnion("numericOrNULL", c("numeric","NULL"))
> > [1] "numericOrNULL"
> > > setClass("foo", representation(x="numericOrNULL"))
> > [1] "foo"
> > > ff = new("foo", x= 1:10)
> > > fg = new("foo", x = NULL)
> > >
> > > ff
> > An object of class "foo"
> > Slot "x":
> > [1] 1 2 3 4 5 6 7 8 9 10
> >
> > > fg
> > An object of class "foo"
> > Slot "x":
> > NULL
> > > fk = new("foo")
> > > fk
> > An object of class "foo"
> > Slot "x":
> > NULL
> >
> > John
> >
> > Sklyar, Oleg (London) wrote:
> >> Dear list:
> >>
> >> I am looking for a good way to create an S4 class that would extend
> >> numeric, but would allow NULL instead of data as well. As 
> far as I can
> >> see there is no way at the moment to do that, but please 
> correct me if I
> >> am wrong. The best solution I came up with so far was the 
> following (it
> >> also indicates a problem of using setClassUnion with 
> numeric as one of
> >> the classes):
> >>
> >> I define a class union of numeric and NULL:
> >>
> >> Unfortunately the following works only with warnings:
> >> setClassUnion("numericOrNULL", c("numeric","NULL"))
> >>
> >> So I do a workaround as:
> >>
> >> setClass("aNumeric", contains="numeric")
> >> setClassUnion("numericOrNULL", c("aNumeric","NULL"))
> >>
> >> Then I cannot really extend the above virtual class and 
> can only use it
> >> in a user-defined slot as follows:
> >>
> >> setClass("myClass", representation(data="numericOrNULL"))
> >> new("myClass", data=runif(20))
> >> new("myClass", data=NULL)
> >>
> >> and this works.
> >>
> >> Obviously it would be nicer to have something like the following:
> >>
> >> setClass("myClass", contains="numericOrNULL")
> >> new("myClass", runif(20)) ## .Data is not a slot of myClass
> >> setClass("myClass", representation("numericOrNULL"))
> >> new("myClass", runif(20)) ## ibid
> >>
> >> Technically I understand that the reason behind it failing 
> to work is
> >> that the virtual class numericOrNULL has not got the .Data 
> slot from
> >> numeric, but it would be nice to have such a functionality.
> >>
> >> Any ideas about better ways for solving such a problem than the one
> >> described above?
> >>
> >> Thanks.
> >>
> >> Best,
> >> Oleg
> >>
> >> Dr Oleg Sklyar
> >> Research Technologist
> >> AHL / Man Investments Ltd
> >> +44 (0)20 7144 3107
> >> osklyar at maninvestments.com
> >>
> >> 
> **********************************************************************
> >> Please consider the environment before printing this email or its 
> >> attachments.
> >> The contents of this email are for the named addressees 
> >> ...{{dropped:19}}
> >>
> >> ______________________________________________
> >> R-devel at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-devel
> >>
> >>
> >
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
> 

**********************************************************************
Please consider the environment before printing this email or its attachments.
The contents of this email are for the named addressees ...{{dropped:19}}



[Rd] setClassUnion with numeric; extending class union
Sklyar, Oleg (London