R6 icon indicating copy to clipboard operation
R6 copied to clipboard

Support for dataclasses

Open avhz opened this issue 10 months ago • 0 comments

Would there be any interest in adding dataclasses as a feature, similar to Python's ?

I find that it would help with avoiding initialize boilerplate for the cases where we just want a structured data container.

Currently:

Foo <- R6::R6Class(
  "Foo",
  public = list(
    x = NULL,
    y = NULL,
    z = NULL,
    initialize = function(x, y, z) {
      self$x <- x
      self$y <- y
      self$z <- z
    }
  )
)

foo <- Foo$new(1, 2, 3)

Hopefully:

Foo <- R6::R6DataClass(
  "Foo",
  public = list(
    x = NULL,
    y = NULL,
    z = NULL,
  )
)

foo <- Foo$new(1, 2, 3)

Edit: or possibly just make initialize optional, with a default created for the user that handles their public fields ?

avhz avatar Feb 26 '25 18:02 avhz