Use the initialize method to coerce arguments. Explicitly exclude
coercions that you do no wish to perform. Coerce using as(x, "class")
instead of trying to construct function names from character strings. Do not re-implement
initFields
.
Part <- setRefClass(Class = "Part",
fields=c(var0 = "ANY", var1 = "numeric", var2 = "character"),
methods=list(
initialize=function(...) {
args <- list(...)
map <- .self$getRefClass()$fields(); map <- map[map != "ANY"]
idx <- names(args) %in% names(map)
args[idx] <- Map(as, args[idx], map[names(args)[idx]])
do.call(.self$initFields, args)
}))
Simple test cases
Part()
Part(var2=1:3)