Open DavisVaughan opened 3 years ago
I think this would be solved by https://github.com/r-lib/vctrs/pull/1231, which adds that missing cast method
But note that [<-.vctrs_list_of
should still be updated or possibly removed entirely
Ah we're already lenient. Then I'm not sure we should make this stricter.
You think [<-
and vec_assign()
should have different behavior? i.e. this looks right to you?
library(vctrs)
x <- list_of(1L, .ptype = integer())
x[1] <- 2
x
#> <list_of<integer>[1]>
#> [[1]]
#> [1] 2
vec_assign(x, 1, 3)
#> Error in `vec_assign()`:
#> ! Can't convert <double> to <list_of<integer>>.
Created on 2022-10-03 by the reprex package (v2.0.1)
Possibly, because [<-
is higher-level than vec_assign()
. The latter is commonly called after types have been straightened out, unlike the former which is exposed to the user.
The downside of making vec_assign()
behave like [<-
is that it's only really possible to handle coercions at the R level. At the C level, that would require a costly indirection around vector storage (for proxy, array pointer, and restore ptype).
Why can we do the first, but not the second?
I think this is because there is no
vec_cast.vctrs_list_of.list
method, so then[<-.vctrs_list_of
tries to make up for this by being a bit more lenient:The logic here is really what would be in a
vec_cast.vctrs_list_of.list
method.