From m@ech|er @end|ng |rom @t@t@m@th@ethz@ch  Thu Mar 28 10:35:57 2019
From: m@ech|er @end|ng |rom @t@t@m@th@ethz@ch (Martin Maechler)
Date: Thu, 28 Mar 2019 10:35:57 +0100
Subject: [Rd] Discrepancy between is.list() and is(x, "list")
In-Reply-To: <CAB8pepxpqGtxC2s0Zs=ZJqqsKa5GVaei1_4-49w0vafmv8J3pA@mail.gmail.com>
References: <CAB8pepyU2qbU7fd1J9J6kyr_657PHFYHoJV6fnvNiHqqjq-24w@mail.gmail.com>
 <EB8494AA-8DAA-4B9A-B636-7E979251EF0C@ucsd.edu>
 <CAB8pepxp9TL18vvHajvSJBXhJY=E13ZWUjebxv6OdgPXcWVPTQ@mail.gmail.com>
 <CAF8bMcZD03odP0yriDjoxYNFzfU3Js_bF6OosavyRxtVY6S44g@mail.gmail.com>
 <CAB8pepxpqGtxC2s0Zs=ZJqqsKa5GVaei1_4-49w0vafmv8J3pA@mail.gmail.com>
Message-ID: <23708.38269.817417.238096@stat.math.ethz.ch>

>>>>> Abs Spurdle 
>>>>>     on Thu, 28 Mar 2019 12:26:36 +1300 writes:

    >> the prison made by ancient design choices
    > That prison of ancient design choices isn't so bad.

    > I have no further comments on object oriented semantics.
    > However, I'm planning to follow the following design
    > pattern.

    > If I set the class of an object, I will append the new
    > class to the existing class.

    > #good class (object) = c ("something", class (object) )

#even better ;-)
      
      class(object) <- c("something", class(object))

    > #bad class (object) = "something"

    > I encourage others to do the same.
Indeed.

BUT also tell the thousands of people who do it -- including
somewhat famous R package authors --

*NOT* to use things such as

      if(class(x) == "Date")

or
      switch(class(x),  
             "Date" = ...... ,
  	     "POSIXct" = ...... ,
	     ....
	     ...
	     stop("invalid class: ", class(x)))

BUT to always use

    inherits(x, "....")

There may be rare exceptions where using   class(x)[1]   is
good, but I have seen many cases where     class(x)[1]  was used
and the R programmers found it smart they knew that
class(x) can be of length more than one, but really their code
would fail *exactly* because good R programmers do *prepend*
their S3 class extension/specialization to the already existing
class.

---

... and then, I do agree with Gabe that (in some cases), using
 formal (aka "S4") classes is really what one should do in order
to get a clean interface.


Martin Maechler
ETH Zurich and R Core Team



[Rd] Discrepancy between is.list() and is(x, "list")
Martin Maechler