Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S7_data<- doesn't allow updating attributes like names #478

Open
t-kalinowski opened this issue Oct 25, 2024 · 0 comments · May be fixed by #479
Open

S7_data<- doesn't allow updating attributes like names #478

t-kalinowski opened this issue Oct 25, 2024 · 0 comments · May be fixed by #479

Comments

@t-kalinowski
Copy link
Member

Note that the name of the 3rd entry is missing after updating.

library(S7)
write_once_list <- new_class("write_once_list",
  class_list,
  constructor = function(...) new_object(list(...)),
  validator = function(self) {
    if(anyDuplicated(nms <- names(self)))
      paste0("names not all unique. duplicates: ",
             paste0(unique(nms[duplicated(nms)]), collapse = ", "))
  }
)

method(`$<-`, write_once_list) <- 
method(`[[<-`, write_once_list) <- function(x, name, value) {
  .x <- S7_data(x)
  if (hasName(.x, name))
    stop("entry exists: ", name)
  .x[[name]] <- value
  S7_data(x) <- .x
  x
}

w <- write_once_list(x = 3, y = 4)

w
#> <write_once_list> List of 2
#>  $ x: num 3
#>  $ y: num 4
w$bar <- 1

w
#> <write_once_list> List of 3
#>  $ x : num 3
#>  $ y : num 4
#>  $ NA: num 1
t-kalinowski added a commit that referenced this issue Oct 25, 2024
@t-kalinowski t-kalinowski linked a pull request Oct 25, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant