[Rd] Is it possible to simply the use of NULL slots (or at least improve the help files)?
Robert Harlow
rh@r|ow86 @end|ng |rom gm@||@com
Fri Sep 25 01:02:39 CEST 2020
It seems one of the primary reasons to use the S4 system is *to be strict
about types.*
IMO the documentation is quite clear on this point, if NULL is not a
subclass of character, which it isn't because inherits(NULL, "character")
is FALSE then everything is behaving exactly as expected and documented.
Between making the calling function responsible for passing an object of
the correct type and setClassUnion, it doesn't seem that complicated to
handle the use case.
On Thu, Sep 24, 2020 at 5:26 PM Martin Morgan <mtmorgan.bioc using gmail.com>
wrote:
> Answering to convey the 'rules' as I know them, rather than to address the
> underlying issues that I guess you are really after...
>
> The S4 practice is to use setOldClass() to explicitly treat an S3
> character() vector of classes as an assertion of linear inheritance
>
> > x <- structure (sqrt (37), class = c ("sqrt.prime", "numeric") )
> > is(x, "maybeNumber")
> [1] FALSE
> > setOldClass(class(x))
> > is(x, "maybeNumber")
> [1] TRUE
>
> There are some quite amusing things that can go on with S3 classes, since
> the class attribute is just a character vector. So
>
> > x <- structure ("September", class = c ("sqrt.prime", "numeric") )
> > is(x, "numeric") ## similarly, inherits()
> [1] TRUE
> > x <- structure (1, class = c ("numeric", "character"))
> > is(x, "numeric")
> [1] TRUE
> > is(x, "character")
> [1] TRUE
>
> Perhaps the looseness of the S3 system motivated the use of setOldClass()
> for anything more than assertion of simple relationships? At least in this
> context setOldClass() provides some type checking sanity
>
> > setOldClass(c("character", "numeric"))
> Error in setOldClass(c("character", "numeric")) :
> inconsistent old-style class information for "character"; the class is
> defined but does not extend "numeric" and is not valid as the data part
> In addition: Warning message:
> In .validDataPartClass(cl, where, dataPartClass) :
> more than one possible class for the data part: using "numeric" rather
> than "character"
>
> Martin Morgan
>
> On 9/24/20, 4:51 PM, "Abby Spurdle" <spurdle.a using gmail.com> wrote:
>
> Hi Martin,
> Thankyou for your response.
>
> I suspect that we're not going to agree on the main point.
> Making it trivially simple (as say Java) to set slots to NULL.
> So, I'll move on to the other points here.
>
> ***Note that cited text uses excerpts only.***
>
> > setClassUnion("character_OR_NULL", c("character", "NULL"))
> > A = setClass("A", slots = c(x = "character_OR_NULL"))
>
> I think the above construct needs to be documented much more clearly.
> i.e. In the introductory and details pages for S4 classes.
> This is something that many people will want to do.
> And BasicClasses or NULL-class, are not the most obvious place to
> start looking, either.
>
> Also, I'd recommend the S4 authors, go one step further.
> Include character_OR_NULL, numeric_OR_NULL, etc, or something similar,
> in S4's predefined basic classes.
> Otherwise, contributed packages will (eventually) end up with hundreds
> of copies of these.
>
> > setClassUnion("maybeNumber", c("numeric", "logical"))
> > every instance of numeric _is_ a maybeNumber, e.g.,
> > > is(1, "maybeNumber")
> > [1] TRUE
>
> > which I think is consistent with the use of 'superclass'
>
> Not quite.
>
> x <- structure (sqrt (37), class = c ("sqrt.prime", "numeric") )
> is (x, "numeric") #TRUE
> is (x, "maybeNumber") #FALSE
>
> So now, an object x, is a numeric but not a maybeNumber.
> Perhaps a class union should be described as a partial imitation of a
> superclass, for the purpose of making slots more flexible.
>
>
> B.
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
[[alternative HTML version deleted]]
More information about the R-devel
mailing list