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

fix unary Ops methods: -, +, ! #490

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

fix unary Ops methods: -, +, ! #490

wants to merge 3 commits into from

Conversation

t-kalinowski
Copy link
Member

No description provided.

@mjskay
Copy link

mjskay commented Nov 8, 2024

I found my way to this PR when I discovered that adding a method for ! did not work in a class I was writing.

From my tests, this PR does not fix this issue for !; e.g. I adapted your test for Double to a Logical and it fails:

Logical := new_class(class_logical)

method(`!`, list(Logical, class_missing)) <- function(e1, e2) {
    Logical(!as.logical(e1))
}
## Error: `generic` is a function, but not an S3 generic function: 
## .Primitive("!")

I think the problem is that "!" %in% group_generics()$Ops is FALSE when it should be TRUE, which stems from the fact that ! is in the Ops S3 group generic but not the Arith, Compare, Logic, or Ops S4 group generics.

As a hackish solution, if I add out$Ops <- union(out$Ops, "!") somewhere after line 116 here:

S7/R/generic-spec.R

Lines 107 to 121 in f75e089

group_generics <- function() {
# S3 group generics can be defined by combining S4 group generics
groups <- list(
Ops = c("Arith", "Compare", "Logic"),
Math = c("Math", "Math2"),
Summary = "Summary",
Complex = "Complex"
)
out <- lapply(groups, function(x) unlist(lapply(x, methods::getGroupMembers)))
if (getRversion() >= "4.3") {
out$matrixOps <- c("%*%")
}
out
}

the test on ! succeeds, though I imagine there should be a more elegant solution.

@t-kalinowski
Copy link
Member Author

Thanks for the report and workaround!

This PR will likely be finished / rolled-into #365.

One workaround you can use until this is fixed is to define an S3 method the S3 way.

#' @export
`!.mypackage::Logical` <- function(x) { .... }

That workaround should work now, and continue to work once this is fixed in S7.

@mjskay
Copy link

mjskay commented Nov 11, 2024

Great thanks! That helps, because I was defining !.ClassName in a previous version of S7 and it stopped working in the latest version; I had missed that the package name is now prepended to the class name.

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 this pull request may close these issues.

2 participants