函数 initFields 不是初始化引用类的任意字段的好方法吗?如果不是,你会建议什么?我怎么能处理“ANY”类型的字段,因为对于字符串“ANY”我不能像do.call(fields[[i]],list())
Part.initFields<-function(args){
argNames <- names(args)
fields<-.self$getRefClass()$fields()
fieldNames <- names(fields)
for(i in seq_along(fields)){
var <- if(is.null(args[[fieldNames[i]]])) do.call(fields[[i]],list())
else do.call(paste("as.",fields[[i]],sep=""),list(args[[fieldNames[i]]]))
assign(fieldNames[[i]], var, attr(.self, ".xData"))
}
}
Part.initialize<-function(...){
args<-list(...)
.self$initFields(args)
.self
}
Part<-setRefClass(Class = "Part"
,fields = c(var1 = "numeric", var2 = "character")
,methods = list(initialize=Part.initialize
,initFields=Part.initFields))
part<-Part$new(var1=1)