Skip to content

'callNextMethod' in a '[<-' function does not work ?

2 messages · Christophe Genolini, Martin Morgan

#
Hi the list,

I try to use callNextMethod in a setteur ([<-) but it does not work. 
Any raison ?
Any other option ?

--- 8< ------------------
### Class B0 ###
setClass("B0" , representation(b0 = "numeric"))
setReplaceMethod("[","B0",function(x,i,j,value){x at b0 <- -value})

a <- new("B0")
a at b0 <- 3
a
a["b0"] <- 3
a


### Class B1 ###
setClass("B1", representation(b1 = "character"), contains = "B0")
setReplaceMethod("[","B1",function(x,i,j,value){
  value <- value*2
  callNextMethod()
})

b <- new("B1")
try(b["b0"] <- 5)
--- 8< ------------------

Christophe

--
View this message in context: http://r.789695.n4.nabble.com/callNextMethod-in-a-function-does-not-work-tp4177669p4177669.html
Sent from the R help mailing list archive at Nabble.com.
#
On 12/09/2011 10:15 AM, cgenolin wrote:
return modified x, and match signature to getGeneric("[<-")

setClass("B0" , representation(b0 = "numeric"))

setReplaceMethod("[","B0", function(x,i,j, ..., value) {
     x at b0 <- -value
     x
})
provide arguments to callNextMethod, reflecting updated value

setClass("B1", representation(b1 = "character"), contains = "B0")

setReplaceMethod("[","B1",function(x,i,j, ..., value){
   value <- value*2
   callNextMethod(x, i, j, ..., value=value)
})
of course "b0" is not being used to index here.

Martin