vctrs icon indicating copy to clipboard operation
vctrs copied to clipboard

`vec_assign()` mutates original rcrd instead of making a copy

Open khusmann opened this issue 1 year ago • 0 comments

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 avatar Aug 15 '24 20:08 khusmann