From hpages at fhcrc.org  Wed Mar  4 19:25:35 2009
From: hpages at fhcrc.org (hpages at fhcrc.org)
Date: Wed, 04 Mar 2009 10:25:35 -0800
Subject: [Rd] "names<-" doesn't raise an error
Message-ID: <20090304102535.e63qkfy58gw0coc8@webmail.fhcrc.org>

Hi,

When a method is not defined for an object, you
expect to get an error. But this is not the case for
"names<-". You can use "names<-" on any S4 object: it
will remain silent, giving you the impression that it
actually did something:

   setClass("A", representation(vals="numeric"))
   setMethod("names", "A", function(x) names(x at vals))

   > vals <- 8:2
   > names(vals) <- letters[1:7]
   > a <- new("A", vals=vals)
   > names(a)
   [1] "a" "b" "c" "d" "e" "f" "g"
   > names(a) <- LETTERS[1:7]         # no error
   > names(a)
   [1] "a" "b" "c" "d" "e" "f" "g"    # nothing has changed

Shouldn't "names<-" return an error like "[" or "[<-" do
in such situation?

   > a[2]
   Error in a[2] : object of type 'S4' is not subsettable
   > a[2] <- 55
   Error in a[2] <- 55 : object of type 'S4' is not subsettable

That would make it more convenient to implement classes where
instances have immutable names. For now I need to use the
following workaround:

   setReplaceMethod("names", "A", function(x, value) stop("cannot set  
my names"))

   > names(a) <- LETTERS[1:7]
   Error in `names<-`(`*tmp*`, value = c("A", "B", "C", "D", "E", "F", "G" :
     cannot set my names

but that shouldn't be necessary.

Thanks!
H.



[Rd] "names<-" doesn't raise an error
hpages at fhcrc.org