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

Compatibility with S4 Methods (feature request/bug) #147

Open
KellenBrosnahan opened this issue Mar 23, 2023 · 2 comments
Open

Compatibility with S4 Methods (feature request/bug) #147

KellenBrosnahan opened this issue Mar 23, 2023 · 2 comments

Comments

@KellenBrosnahan
Copy link

Hello,
I am currently writing a package using the S4 OOP system, and was wondering whether there was an existing canonical way to memoize S4 methods. One of the errors I got was that setMethod cannot be passed a memoized function because the S3 class memoised is not technically an S4 class extending function, which is what the MethodDefinition class (for S4 methods) requires:

Error in initialize(value, ...) : 
  cannot use object of class “memoised” in new():  class “MethodDefinition” does not extend that class

A simple workaround is to formally declare memoised to be an S4 class extending function via the following code:

setClass("memoised", contains = "function", slots = character(), prototype = new("function"))

Is there a reason not to do this? Should this be part of the memoise package (assuming methods is loaded)?

Here's a reprex:

f <- function(x){x}
memF <- memoise::memoise(f)
setClass("TestClass", slots = character(), prototype = list())
setGeneric("testMethod", function(x) standardGeneric("testMethod"))
setMethod("testMethod", signature = "TestClass", memF)
@wch
Copy link
Member

wch commented Mar 24, 2023

Sounds reasonable to me (but I am also not an S4 expert).

@hadley What do you think?

@hadley
Copy link
Member

hadley commented Mar 24, 2023

If we were to do that, we'd use setOldClass(), but that sort of S4 manipulation can have unexpected and far reaching complications, so I wouldn't recommend it.

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

No branches or pull requests

3 participants