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

Martin Maechler m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Thu Mar 28 10:35:57 CET 2019


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



More information about the R-devel mailing list