RConsortium / S7

S7: a new OO system for R
https://rconsortium.github.io/S7
Other
399 stars 33 forks source link

In place mutability of S7 objects #437

Closed JosiahParry closed 1 month ago

JosiahParry commented 1 month ago

Is there a road map to offer in-place mutation of an S7 object?

Ideally, I would be able to create a method that update the value of the S7 object while returning another object.

Here is a bit of a contrived example based on what I'm working on.

my_job <- new_class("my_job", properties = list(status = class_character))

job_status <- new_generic("job_status", "job")

method(job_status, my_job) <- function(job) {
  # do something 
  new_status <- "computing"
  job@status <- new_status

  list(status = "computing", value = "some other derived value")
}
JosiahParry commented 1 month ago

I do note that the proposal says

"We exclude from our scope systems with mutable objects, because immutable objects are generally preferable for interactive data analysis, relegating mutable systems to niche applications, such as GUIs and caching mechanisms."

but does this preclude it from ever being included in S7?

R6 does not have the niceties of automatic validation which is why I am seeking to use S7—type safety is preferred whenever possible :)

hadley commented 1 month ago

We might one day introduce something like R6, but I suspect that's far in the future. For now, you can always make a property that is an environment (although then you do lose all the property validation niceness).

t-kalinowski commented 1 month ago

See also: https://github.com/RConsortium/S7/issues/202