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

vec_assign() mutates original rcrd instead of making a copy #1950

Open
khusmann opened this issue Aug 15, 2024 · 0 comments
Open

vec_assign() mutates original rcrd instead of making a copy #1950

khusmann opened this issue Aug 15, 2024 · 0 comments

Comments

@khusmann
Copy link

vec_assign() should not mutate values, and instead return a modified copy of the original vctr (as illustrated in the examples). But this breaks for rcrd types (or more generally, where the vec_proxy is a dataframe). In these cases, the original rcrd gets modified by vec_assign():

library(vctrs)

new_foo <- function(a, b) {
  new_rcrd(list(a = a, b = b), class = "foo")
}

format.foo <- function(x, ...) {
  format(vec_proxy(x))
}

foo <- new_foo(a = c(1, 2), b = c(3, 4))

foo
#> <foo[2]>
#>      
#> 1 1 3
#> 2 2 4

# This assignment should not mutate foo:
bar <- vec_assign(foo, 2, new_foo(a=9000, b=9000))

foo # yikes, foo was mutated! spooky action at a distance!
#> <foo[2]>
#>            
#> 1    1    3
#> 2 9000 9000

Created on 2024-08-15 with reprex v2.0.2

khusmann added a commit to khusmann/interlacer that referenced this issue Aug 16, 2024
khusmann added a commit to khusmann/interlacer that referenced this issue Sep 5, 2024
khusmann added a commit to khusmann/interlacer that referenced this issue Sep 5, 2024
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

No branches or pull requests

1 participant