How to define several polymorph constructors and functions like
function Add( x, y : Integer ) : Integer; begin Add := x + y end; function Add( s, t : String ) : String; begin Add := Concat( s, t ) end; begin Writeln(Add(1, 2)); Writeln(Add('Hello, ', 'World!')); end.
Can I do this only by case decission like
A<-setRefClass(Class = "A"
,fields = list(var1="character")
,methods = list(setFields=A.setFields
,initialize=function(var1) {
if(isClass(var1,"B"))
.self$var1<-as.character(var1$getFields("var1"))
else{
.self$var1<-as.character(var1)
}
.self
})
)
How to combine functional programming with objectorientated prgramming. So if I would call the functions getFields(vecB), where vecB is a vector or list of objects B. The return should be the values of each object?
B.getFields<-function(...,values){ vars<-mget(names(.refClassDef@fieldClasses), envir = attr(.self, ".xData")) if(missing(values)) return(vars) if(length(vars)==1) return(vars[[1]]) return(vars[names(vars) %in% values]) } B<-setRefClass(Class = "B" ,fields = list(var1 = "character") )
How to debug e.g. the function initialize from class A? I tried
A$trace("initialize") a<-A$new("ABC") initial<-a$initialize trace(initial,browser,where=A)
but it doesnt work.
Use S4 generics and methods for polymorphism
so
One idea to use this generic in a reference class is
with appropriate dispatch for functional program style:
(maybe for functional programming it makes more sense to implement
$addX()
asA(.self, x=Add(x$getX(), y))
, i.e., creating a clone of x?) to allow forthough not
A(x=1)$addX(A(x=2))
See this answer for one approach to returning field values:
One way to invoke in a functional way requires a separate implementation, e.g.,
Your class A example is incomplete.
and then