Skip to content

Commit

Permalink
Move generic checking earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Nov 27, 2023
1 parent 310acdf commit c5e6d22
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
6 changes: 2 additions & 4 deletions R/method-introspect.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#' try(method(bizarro, class = class_data.frame))
#' try(method(bizarro, object = "x"))
method <- function(generic, class = NULL, object = NULL) {
generic <- as_generic(generic)
check_is_S7(generic, S7_generic)
dispatch <- as_dispatch(generic, class = class, object = object)

method <- .Call(method_, generic, dispatch, environment(), FALSE)
Expand Down Expand Up @@ -78,7 +78,7 @@ method <- function(generic, class = NULL, object = NULL) {
#'
#' method_explain(add, list(foo2, foo2))
method_explain <- function(generic, class = NULL, object = NULL) {
generic <- as_generic(generic)
check_is_S7(generic, S7_generic)
dispatch <- as_dispatch(generic, class = class, object = object)
dispatch <- lapply(dispatch, c, "ANY")

Expand Down Expand Up @@ -108,8 +108,6 @@ method_explain <- function(generic, class = NULL, object = NULL) {


as_dispatch <- function(generic, class = NULL, object = NULL) {
check_is_S7(generic, S7_generic)

if (!is.null(class) && is.null(object)) {
signature <- as_signature(class, generic)
is_union <- vlapply(signature, is_union)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/method-introspect.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
method(print, 1)
Condition
Error:
! `generic` must be a <S7_generic>, not a S3<S7_S3_generic>
! `generic` must be a <S7_generic>, not a <closure>
Code
foo <- new_generic("foo", "x")
method(foo)
Expand Down

0 comments on commit c5e6d22

Please sign in to comment.