Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions R/method-ops.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ on_load_define_ops <- function() {
#' @export
Ops.S7_object <- function(e1, e2) {
cnd <- tryCatch(
return(base_ops[[.Generic]](e1, e2)),
return(
if (missing(e2))
base_ops[[.Generic]](e1)
Comment thread
hadley marked this conversation as resolved.
Outdated
else
base_ops[[.Generic]](e1, e2)
),
S7_error_method_not_found = function(cnd) cnd
)

if (S7_inherits(e1) && S7_inherits(e2)) {
if (S7_inherits(e1) && (missing(e2) || S7_inherits(e2))) {
stop(cnd)
} else {
# Must call NextMethod() directly in the method, not wrapped in an
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-method-ops.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,13 @@ test_that("Ops methods can use super", {

expect_equal(foo2(1L) + 1, foo2(2L))
})


test_that("Unary Ops methods work", {
Double := new_class(class_double)
method(`-`, list(Double, class_missing)) <- function(e1, e2) {
Double(-as.double(e1))
}

expect_identical(-Double(1), Double(-1))
})