diff --git a/R/method-ops.R b/R/method-ops.R index a39ee00b..0617940c 100644 --- a/R/method-ops.R +++ b/R/method-ops.R @@ -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) + 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 diff --git a/tests/testthat/test-method-ops.R b/tests/testthat/test-method-ops.R index d825b65e..2dfd1fec 100644 --- a/tests/testthat/test-method-ops.R +++ b/tests/testthat/test-method-ops.R @@ -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)) +})