[R] setAs vs setIs

Martin Maechler maechler at stat.math.ethz.ch
Mon Mar 17 10:33:39 CET 2008


>>>>> "CG" == Christophe Genolini <cgenolin at u-paris10.fr>
>>>>>     on Mon, 17 Mar 2008 09:42:09 +0100 writes:

    CG> I works, great !
    CG> So using your code, we can define 'as','as<-' and 'is' with setIt. Is 
    CG> there still any interest using setAs ?

Well, ?setIs  contains 

  >>  This function establishes an inheritance relation
  >>  between two classes, by some means other than having one
  >>  class contain the other.  It should _not_ be used for
  >>  ordinary relationships: either include the second class in
  >>  the 'contains=' argument to 'setClass' if the class is
  >>  contained in the usual way, or consider 'setClassUnion' to
  >>  define a virtual class that is extended by several ordinary
  >>  classes.  A call to 'setIs' makes sense, for example, if one
  >>  class ought to be automatically convertible into a second
  >>  class, but they have different representations, so that the
  >>  conversion must be done by an explicit computation, not just
  >>  be inheriting slots, for example.  In this case, you will
  >>  typically need to provide both a 'coerce=' and 'replace='
  >>  argument to 'setIs'.

which for me has been entailing that you should use
setIs() only rarely but rather

use  setClass(..., contains = ....)
and  setAs(.....)

much more frequently.  That may well depend on the kind of your
classes of course.
For the 'Matrix' package at least, our experience and use
testing has resulted in almost no use of setIs()
but rather hierarchical class definitions, i.e., the above 
setClass( ..., contains=), and many setAs(...).

Martin Maechler, ETH Zurich



More information about the R-help mailing list