Given an R6 class Class1
with its initialize
function, I would like to be able to pass an instance of the same class and return it directly.
Something like this:
if("Class1" %in% class(x)) x
else Class1$new(x)
But inside the initialize
function of an R6 class, that should work like this
# This script does not work
# Definition of the class
Class1 = R6::R6Class("Class1",
public=list(
initialize = function(x){
# This line does not work
if("Class1"%in%class(x)) return(x)
}
))
# Initiate an instance from scratch
foo = Class1$new()
# Initiate an instance through another instance
bar = Class1$new(foo)
# The two should be the same
identical(foo, bar)
#> TRUE