I am writing a package and need to decide whether to use S3 or S4. I have a single class, "multipol"; this needs methods for "[" and "[<-" and I also need a print (or show) method and methods for arithmetic +- */^. In S4, an object of class "multipol" has one slot that holds an array. Objects of class "multipol" require specific arithmetic operations; a,b being multipols means that a+b and a*b are defined in peculiar ways that make sense in the context of the package. I can also add and multiply by scalars (vectors of length one). My impression is that S3 is perfectly adequate for this task, although I've not yet finalized the coding. S4 seems to be "overkill" for such a simple system. Can anyone give me some motivation for persisting with S4? Or indeed reassure me that S3 is a good design decision? -- Robin Hankin Uncertainty Analyst and Neutral Theorist, National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
On Mon, 7 Jan 2008, Robin Hankin wrote:> I am writing a package and need to decide whether to use S3 or S4. > > I have a single class, "multipol"; this needs methods for "[" and "[<-" > and I also need a print (or show) method and methods for arithmetic +- > */^. > > In S4, an object of class "multipol" has one slot that holds an array. > > Objects of class "multipol" require specific arithmetic operations; > a,b being > multipols means that a+b and a*b are defined in peculiar ways > that make sense in the context of the package. I can also add and > multiply > by scalars (vectors of length one). > > My impression is that S3 is perfectly adequate for this task, although > I've not yet finalized the coding. > > S4 seems to be "overkill" for such a simple system. > > Can anyone give me some motivation for persisting with S4? > > Or indeed reassure me that S3 is a good design decision?Does performance matter?: S4 dispatch is many times slower than S3 dispatch for such functions. (It is several times slower in general, but the difference is particularly marked for primitives.) -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Would you like existing functions such as mean, range, sum, colSums, dim, apply, length, and many more to operate on the array of numbers? If so use an S3 class. If you would like to effectively disable such functions, to prevent them from working on the object unless you write a method that specifies exactly how the function should operate on the class, then either use an S4 class, or an S3 class where the array is one component of a list. An S3 class also allows for flexibility - you can add attributes, or list components, without breaking things. As for reassurance - I use S3 classes for almost everything, happily. The one time I chose to use an S4 class I later regretted it. This was for objects containing multiple imputations, where I wanted to prevent functions like mean() from working on the original data, without filling in imputations. The regret was because we later realized that in some cases we wanted to add a "call" attribute or component/slot so that update() would work. If it had been an S3 object we could have done so, but as an S4 object we would have broken existing objects of the class. Tim Hesterberg Disclaimer - this is my personal opinion, not my employer's.>I am writing a package and need to decide whether to use S3 or S4. > >I have a single class, "multipol"; this needs methods for "[" and "[<-" >and I also need a print (or show) method and methods for arithmetic +- >*/^. > >In S4, an object of class "multipol" has one slot that holds an array. > >Objects of class "multipol" require specific arithmetic operations; >a,b being >multipols means that a+b and a*b are defined in peculiar ways >that make sense in the context of the package. I can also add and >multiply >by scalars (vectors of length one). > >My impression is that S3 is perfectly adequate for this task, although >I've not yet finalized the coding. > >S4 seems to be "overkill" for such a simple system. > >Can anyone give me some motivation for persisting with S4? > >Or indeed reassure me that S3 is a good design decision?