Message-ID: <971536df050507085659082061@mail.gmail.com>
Date: 2005-05-07T17:56:37Z
From: Gabor Grothendieck
Subject: how to add method to .Primitive function
In-Reply-To: <C698D707214E6F4AB39AB7096C3DE5A59E9333@phost015.EVAFUNDS.intermedia.net>
On 5/7/05, Vadim Ogranovich <vograno@evafunds.com> wrote:
> I tried to write the dim method for the list class, but R doesn't seem
> to dispatch to it:
> > dim.list = function(x) c(length(x[[1]]), length(x))
> > dim(list(1))
> NULL
> > dim.list(list(1))
> [1] 1 1
>
> What is the correct way of registering dim.list with .Primitive("dim")?
The list method of dim is sealed
> isSealedMethod("dim", "list")
[1] TRUE
but you could define a subclass, mylist, and use that:
> setClass("mylist", representation(x = "list"), contains = "list")
[1] "mylist"
> setMethod("dim", "mylist", function(x) c(length(x@x[[1]]), length(x@x)))
[1] "dim"
> x <- new("mylist", x = list(x = 1, y = 2))
> dim(x)
[1] 1 2