[Rd] as.list method for by Objects

Henrik Bengtsson henrik.bengtsson at gmail.com
Sat Feb 3 22:25:55 CET 2018


On Fri, Feb 2, 2018 at 12:07 AM, Martin Maechler
<maechler at stat.math.ethz.ch> wrote:
>>>>>> Henrik Bengtsson <henrik.bengtsson at gmail.com>
>>>>>>     on Thu, 1 Feb 2018 10:26:23 -0800 writes:
>
>     > On Thu, Feb 1, 2018 at 12:14 AM, Martin Maechler
>     > <maechler at stat.math.ethz.ch> wrote:
>     >>>>>>> Michael Lawrence <lawrence.michael at gene.com>
>     >>>>>>> on Tue, 30 Jan 2018 15:57:42 -0800 writes:
>     >>
>     >> > I just meant that the minimal contract for as.list() appears to be that it
>     >> > returns a VECSXP. To the user, we might say that is.list() will always
>     >> > return TRUE.
>     >>
>     >> Indeed. I also agree with Herv'e that the user level
>     >> documentation should rather mention  is.list(.) |--> TRUE  than
>     >> VECSXP, and interestingly for the experts among us,
>     >> the  is.list() primitive gives not only TRUE for  VECSXP  but
>     >> also of LISTSXP (the good ole' pairlists).
>     >>
>     >> > I'm not sure we can expect consistency across methods
>     >> > beyond that, nor is it feasible at this point to match the
>     >> > semantics of the methods package. It deals in "class
>     >> > space" while as.list() deals in "typeof() space".
>     >>
>     >> > Michael
>     >>
>     >> Yes, and that *is* the extra complexity we have in R (inherited
>     >> from S, I'd say)  which ideally wasn't there and of course is
>     >> not there in much younger languages/systems such as julia.
>     >>
>     >> And --- by the way let me preach, for the "class space" ---
>     >> do __never__ use
>     >>
>     >> if(class(obj) == "<classname>")
>     >>
>     >> in your code (I see this so often, shockingly to me ...) but rather use
>     >>
>     >> if(inherits(obj, "<classname>"))
>     >>
>     >> instead.
>
>     > Second this one.  But, soon (*) the former will at least give the
>     > correct answer when length(class(obj)) == 1
>     > and produce an error
>     > otherwise.
>
> Not quite; I think you you did not get the real danger in using
> 'class(.) == *':
> What you say above would only be true if there were only S3 classes!
> Try the following small R snippet
>
> myDate <- setClass("myDate", contains = "Date")
> ## Object of class "myDate"
> ## [1] "2018-02-02"
> (d <- myDate(Sys.Date()))
> class(d) == "Date"  # is FALSE (hence of length 1)
> inherits(d, "Date") # is TRUE
>
>     > So, several of these cases will be caught at run-time in a
>     > near future.
>
> Maybe.  But all the others are  still wrong, as I show above.

Oh my, thanks for clarifying/emphasizing.  I hope I didn't mislead too
many people.  I've been away from S4 for too long - I like to stay in
the cozy S3 world :)

/Henrik

> Martin
>
>     > (*) When _R_CHECK_LENGTH_1_CONDITION_=true becomes the default
>     > behavior - hopefully by R 3.5.0.
>
>     >>
>     >> Martin



More information about the R-devel mailing list