It looks like a sound solution.
Thanks Vitalie.
Vitalie S. wrote:
Hi,
RG> Hi,
RG> I'm wondering if the following behaviour is normal:
RG> setClass('A', representation(number='numeric'), RG>
validity=function(object){ RG> if( obj...@number < -1 )
return("Invalid number"); TRUE})
>> [1] "A"
RG> a <- new('A')
RG> a
>> An object of class “A”
>> Slot "number":
>> numeric(0)
RG> a...@number <- 0
RG> a...@number <- -3
RG> a
>> An object of class “A”
>> Slot "number":
>> [1] -3
Rewriting `@<-` will do the job:
setClass('A', representation(number='numeric'),
validity=function(object){
if( obj...@number < -1 ) return("Invalid number")
TRUE}
)
setMethod("@<-",
signature(object = "A"),
function (object, name, value){
object <- callNextMethod(object,
as.character(substitute(name)), value)
validObject(object)
return(object)
}
)
a <- new('A')
a...@number <- 23
a...@number <- -23
Error in validObject(object) : invalid class "A" object: Invalid number
By th way, it seems (to me) that there is a bug in callNextMethod above:
object <- callNextMethod(object, name, value)
or
object <- callNextMethod()
trigger an error:
Error in checkSlotAssignment(object, name, value) :
"name" is not a slot in class "A"
-Vitalie
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.