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

Avoid inheritance in double dispatch #710

Closed
lionel- opened this issue Dec 19, 2019 · 2 comments · Fixed by #963
Closed

Avoid inheritance in double dispatch #710

lionel- opened this issue Dec 19, 2019 · 2 comments · Fixed by #963

Comments

@lionel-
Copy link
Member

lionel- commented Dec 19, 2019

By guarding our methods and generics with inherits_only():

vec_ptype2.data.table <- function(x, y, ..., x_arg = "x", y_arg = "y") {
  if (inherits_only(x, c("data.table", "data.frame"))) {
    UseMethod("vec_ptype2.data.table", y)
  } else {
    vec_default_ptype2(x, y, x_arg = x_arg, y_arg = y_arg)
  }
}
vec_ptype2.data.table.data.frame <- function(x, y, ..., x_arg = "x", y_arg = "y") {
  if (inherits_only(y, "data.frame")) {
    x
  } else {
    vec_default_ptype2(x, y, x_arg = x_arg, y_arg = y_arg)
  }
}

vec_ptype2.data.frame.data.table <- function(x, y, ..., x_arg = "x", y_arg = "y") {
  # The `x` check should be taken care of `vec_ptype2.data.frame()`
  if (inherits_only(x, "data.frame") && inherits_only(y, c("data.table", "data.frame"))) {
    y
  } else {
    vec_default_ptype2(x, y, x_arg = x_arg, y_arg = y_arg)
  }
}

This means that we force subclasses to write common type methods with their superclasses, but in return we gain type stability. If we allow inheritance we get:

dt <- data.table::as.data.table(mtcars)
gdf <- dplyr::group_by(mtcars, cyl)

# This would return a `dt` because `gdf` inherits from data.frame
vec_ptype2(dt, gdf)
# And this would return a `gdf` because `dt` inherits from data.frame
vec_ptype2(gdf, dt)

We should try and offer a better common type mechanism in the long term, but this approach would at least make things more predictable in the short term.

@DavisVaughan
Copy link
Member

Small comment. I assume it would return vec_ptype(x) not x when the inherits_only() case is true.

@lionel-
Copy link
Member Author

lionel- commented Dec 19, 2019

Unclear whether we should return the base super type data.frame() instead of throwing a incompatible type error. This would make it possible to combine a dt and a gdf, but we'd silently lose the grouping structure. Probably better to be stricter for now.

Regarding vec_cast(), we should avoid inheritance for to, but it should be fine to allow it for x.

@lionel- lionel- mentioned this issue Feb 27, 2020
lionel- added a commit to lionel-/vctrs that referenced this issue Mar 16, 2020
lionel- added a commit to lionel-/vctrs that referenced this issue Mar 17, 2020
lionel- added a commit to lionel-/vctrs that referenced this issue Mar 27, 2020
lionel- added a commit to lionel-/vctrs that referenced this issue Mar 27, 2020
lionel- added a commit to lionel-/vctrs that referenced this issue Mar 27, 2020
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.

2 participants