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

vctrs_list_of and subset assignment #1371

Open
krlmlr opened this issue Apr 26, 2021 · 0 comments
Open

vctrs_list_of and subset assignment #1371

krlmlr opened this issue Apr 26, 2021 · 0 comments

Comments

@krlmlr
Copy link
Member

krlmlr commented Apr 26, 2021

Do I need to implement more methods to make subset assignment work with "list of" classes? I wonder why my vec_cast() implementation doesn't fire.

Works with atomic vector

library(vctrs)
new_percent <- function(x = double()) {
  vec_assert(x, double())
  new_vctr(x, class = "vctrs_percent")
}

vec_ptype2.vctrs_percent.vctrs_percent <- function(x, y, ...) new_percent()

vec_cast.vctrs_percent.vctrs_percent <- function(x, to, ...) x

pct <- new_percent(1.5:10)
pct[1] <- 2

Fails with list_of

library(vctrs)
new_percent <- function(x = list()) {
  vec_assert(x, list())
  new_list_of(x, double(), class = "vctrs_percent")
}

vec_ptype2.vctrs_percent.vctrs_percent <- function(x, y, ...) new_percent()

vec_cast.vctrs_percent.vctrs_percent <- function(x, to, ...) {
  message("Casting")
  x
}

pct <- new_percent(as.list(1.5:10))
pct[1] <- new_percent(list(2))
#> Error: Can't convert <list_of<double>> to <list_of<double>>.

Created on 2021-04-26 by the reprex package (v2.0.0)

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