From maechler at stat.math.ethz.ch  Fri Feb  2 09:07:44 2018
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Fri, 2 Feb 2018 09:07:44 +0100
Subject: [Rd] as.list method for by Objects
In-Reply-To: <CAFDcVCRvmKrFMuoEDBVjaKEdEc6=wbHGfJQBSDBakSNRCGc0oA@mail.gmail.com>
References: <SY3PR01MB07474F9CEBBC61FD2A152E58CDE40@SY3PR01MB0747.ausprd01.prod.outlook.com>
 <02275be4-b015-0860-8733-efb2b6b17996@fredhutch.org>
 <CADwqtCO98pTE6G4xOva3pVEEA0jSMND9zepcR19B-8rZ6mhYRg@mail.gmail.com>
 <f2080a27-71e0-4f52-61cd-2c2a8bb8e8cd@fredhutch.org>
 <CAOQ5Nye8vFGjheY+QW1H5-Am6BN+V_jKGuCe8=_YbeMQyQFGkA@mail.gmail.com>
 <74406925-3fbb-67d6-5afa-2d18f0b00513@fredhutch.org>
 <CAOQ5NyegUu+9sGCuT4HZpP8vqJncPB5TckLXteS2zNSdtE9zTQ@mail.gmail.com>
 <23154.52332.796492.172488@stat.math.ethz.ch>
 <CAFDcVCRvmKrFMuoEDBVjaKEdEc6=wbHGfJQBSDBakSNRCGc0oA@mail.gmail.com>
Message-ID: <23156.7248.538523.4058@stat.math.ethz.ch>

>>>>> 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.
Martin

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

    >> 
    >> Martin



[Rd] as.list method for by Objects
Martin Maechler