Skip to content
Prev 17134 / 63421 Next

S4 setClass with prototypes " issues" (PR#8053)

Well, let's say R is currently picky when only a prototype is supplied.

The following is either a workaround or a revised, fussier requirement 
for the example mentioned, depending on your interpretation.

R> setClass("sequence", representation(.Data="numeric"), 
prototype=numeric(3))
[1] "sequence"
R> xx <- new('sequence',c(1,51,10))
R> xx
An object of class ?sequence?
[1]  1 51 10
R> is(xx, "numeric")
[1] FALSE

So far, so good.  But there are a couple of catches.

The resolution of prototype and representation is currently somewhat of 
a mess in the R implementation.  There's been some discussion of 
cleaning it up, hopefully moving ahead from the Green Book description 
to a more coherent definition.  So whether eventually one could (or 
should) omit the representation() part remains to be seen.

The other relevant flaw currently is that S4 objects have no special 
internal representation in R, so there's effectively no way to keep 
primitive operations from working on them. (The general & notorious 
example is that xx[] always returns something on an S4 object, even when 
it shouldn't.)  In the current case, the problem is that, in spite of 
not extending "numeric", low-level arithmetic is still done.

R> xx+1
An object of class ?sequence?
[1]  2 52 11

Something basic is needed here, in the code for primitives, but so far 
objections re efficiency have prevented doing anything.

Meanwhile, those more interested in getting something done than 
discussing, would need to implement explicit methods for the new class 
that either re-interpret or block the primitives that shouldn't happen 
in the standard way.
gunter.berton at gene.com wrote: