Skip to content

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

@KellenBrosnahan

Description

@KellenBrosnahan

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions