Re: [R] setAs vs setIs

From: Martin Maechler <maechler_at_stat.math.ethz.ch>
Date: Mon, 17 Mar 2008 10:33:39 +0100

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



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 17 Mar 2008 - 09:37:49 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 17 Mar 2008 - 10:30:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive