[Rd] slots of type "double"

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Nov 13 16:37:16 CET 2007


On Tue, 13 Nov 2007, Simon Urbanek wrote:

>
> On Nov 13, 2007, at 3:36 AM, hpages at fhcrc.org wrote:
>
>> Any idea why S4 doesn't allow slots of type "double"?
>>
>
> Type (as in storage type and "double" is a storage type) has nothing
> to do with classes. You cannot create slots for types, only for classes.

Unfortunately not in the world of the 'methods' package (which seems to 
differ here from the Green Book and S-PLUS).  There's a basic class 
"double" (and also "single") defined in BasicClasses.R.  It say:

> getClass("double")

No Slots, prototype of class "numeric"

Extends: "vector", "numeric"

> getClass("numeric")

No Slots, prototype of class "numeric"

Extends: "vector"

Known Subclasses: "double", "integer"

So it is an otherwise unspecified subclass of "numeric".

Now when you do

> a <- new("numeric", pi)
> a
[1] 3.141593
> class(a)
[1] "numeric"

you are in fact calling class() on a REALSXP, and that is set up to report 
"numeric" (the implicit class comes from mode not type, except for 
"integer").

Anther area of confusion is that as.numeric, as.double and as.real are 
three names for the same thing, but is.numeric and is.double are not the 
same.  So we end up with

> x <- 1:3
> is.numeric(x)
[1] TRUE
> identical(x, as.numeric(x))
[1] FALSE
> x <- pi
> is.numeric(x)
[1] TRUE
> identical(x, as.numeric(x))
[1] TRUE

Basically I think class() is wrong (it should use "double"), but it was 
not my choice and it was presumably done to agree with the Green Book.

I agree that it would be best to remove the "double" and "single" S4 
classes.  At least in the version of S-PLUS I have left, "double" does not 
exist -- "single" does but is a distinct storage type.


>
>
>>> setClass("A", representation(a="double"))
>>  Error in makePrototypeFromClassDef(properties, ClassDef, immediate,
>> where) :
>>    in making the prototype for class "A" elements of the prototype
>> failed to
>> match the corresponding slot class: a (class ?double? )
>>
>> "numeric", "integer", "character", "complex", "raw", etc... they all
>> work but "double" doesn't. Any reason for this strange exception?
>>
>
> AFAICS there is no way to create an object of the class "double" (save
> for 'faking' it by creating an informal S3 object), so such definition
> is useless. And given this fact, even the internal code is unable to
> create it, so it doesn't match the signature. Note:
>
> > a=new("double")
> > class(a)
> [1] "numeric"
> > is(a,"double")
> [1] FALSE
>
> I think it comes from the fact that there is an S4 definition for the
> class "double" which is not valid. That is probably a bug and the
> class definition should be removed.
>
> Cheers,
> Simon
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list