I replied off list, accidentally, below. Christophe then asked
>> Probably you want to write a method for 'length' with signature
x = 'E'.
>
> Well you're right. Consider the following code :
>
>
setClass("A",representation(nb="numeric",li="list"))
> setMethod(
> "show",
> "A",
> function(object){
> for(i in 1:object at nb){print(object at li[i])}
> }
> )
You want to use seq_along(object at li) rather than 1:object at nb (which
fails when object at nb is 0-length)
> x <- list()
> for (i in seq_along(x)) cat(x[[i]], "\n")
>
(no error, no output). 'for (i in 1:x)'-style iterations also surprise
when x=0. Also, 'cat' is used more often in 'show' methods,
perhaps
because it imposes less formating on the output and hence allows the
'show' method to have more control over compactly representing the
complex S4 objects.
> # this works
> new("A",nb=2,li=list("E","R"))
>
> # this does not
> new("A")
>
> So I want to define show with first detecting if the object A is the empty
object or not.
>
>> Probably you want to write a method for 'length' with signature
x = 'E'. > slotNames and getSlots might help you to write a function
that visits all slots.
>
> Reading what you say (but I did non try yet), I think to define a length
function for class "A" that will be the sum of the length of all the
slot, then I will test length(a)!=0
>
> Christophe
[off-list reply, oops]
Christophe Genolini wrote:
> Hi the list,
>
> With basic type, length gives the length of the vector
> Wtih list, length gives the number of item
> With S4 object, length gives...one. Even with an objet with empty slot.
not quite; if the class extends a class for which there is a length
method, then, e.g.,
> setClass("E", "numeric")
[1] "E"
> length(new("E"))
[1] 0
Probably you want to write a method for 'length' with signature x =
'E'.
slotNames and getSlots might help you to write a function that visits
all slots. object.size will give you the physical size of the object,
but this is probably not what you want.
Martin
> setClass("E",representation(e="numeric"))
> [1] "E"
> length(new("E"))
> [1] 1
> setClass("E",representation(e="matrix"))
> [1] "E"
> length(new("E"))
> [1] 1
>
> Is there a way to get the real length of an S4 objet ? Furthermore,
is there a simple way to detect that an object is has all its slot to
object of size zero ?
>
> Thanks
>
> Christophe