constructive
                                
                                
                                
                                    constructive copied to clipboard
                            
                            
                            
                        S4 prototype constructor + our S4 construction is not robust
Currently we assume a default initialise() method so new() maps its inputs to attributes values, this is not general!
track <- setClass("track", slots = c(x="numeric", y="numeric"))
setMethod(
  "initialize", "track",
  function(.Object, x = numeric(0), y = numeric(0), ...) {
    # switching values
    .Object@x <- y
    .Object@y <- x
    .Object
  }
)
t1 <- track(x = 1:10, y = 2:11)
constructive::construct(t1, check = TRUE)
#> new("track", x = 2:11, y = 1:10)
#> Error in `check_round_trip()` at constructive/R/construct.R:136:3:
#> ! {constructive} couldn't create code that reproduces perfectly the input
#>  `original@x[1:3]`:   2 3 4
#> `recreated@x[1:4]`: 1 2 3 4
#>  `original@x[7:10]`: 8 9 10 11
#> `recreated@x[8:10]`: 8 9 10   
#>  `original@y[1:4]`: 1 2 3 4
#> `recreated@y[1:3]`:   2 3 4
#>  `original@y[8:10]`: 8 9 10   
#> `recreated@y[7:10]`: 8 9 10 11
Created on 2025-01-17 with reprex v2.1.0
We should check if the initialise method is implemented, and if it is, fall back on a new method "prototype" to be implemented, this method would start from getClass("S4")@prototypeand add attributes.