Skip to content

S7::method does not accept an S3 generic while the doc suggest it should. #568

Closed

Description

@lgautier

In R, with S7 v0.2.0:

> isS3stdGeneric(print)
print 
 TRUE 
> S7::method(print)
Error: `generic` must be a <S7_generic>, not a <closure>

The doc says:

Usage:

     method(generic, class = NULL, object = NULL)
     
Arguments:

 generic: A generic function, i.e. an S7 generic, an external generic,
          an S3 generic, or an S4 generic.

Activity

  1. kevinushey commented on Dec 8, 2025

    @kevinushey

    +1. It's also awkward that one can define an S3 method in this way:

    > s7mean <- S7::new_class("s7mean")
    > S7::method(mean, s7mean) <- function(x, ...) {
    +    print("This is my S7 method")
    + }

    And yet you cannot retrieve it using the same syntax:

    > S7::method(mean, s7mean)
      Error: `generic` must be a <S7_generic>, not a <closure>
  2. mmaechler commented on Dec 18, 2025

    @mmaechler
    Collaborator

    method<- is defined in S7 and in some sense is a major "feature" of S7, using an S- and R- like syntax to change / create an extension / extra part of an object (like, e.g., names(obj) <- value).

    I've edited my original comment now, which contained considerable nonsense, I'm sorry.

    We do have method in S7 (and not in base, I was confusing it with methods()),
    and indeed, @kevinushey 's example to support S7 methods for base / S3 generics does make a lot of sense, unless we'd want to forbid the creation of such methods.

  3. hadley commented on May 21, 2026

    @hadley
    Member

    I think we'll fix this with documentation. To make life easier for developers, we allow method<- to work whenever at least one of the inputs is S7. But I don't think we want to make S7 method introspection also work for every OO system.

  4. added
    feature
    and removed on May 21, 2026
  5. hadley commented on May 21, 2026

    @hadley
    Member

    Looking at the totality of other issues, I now believe we should just implement this.

  6. hadley commented on May 22, 2026

    @hadley
    Member

    In general, it should accept any class specification.

  7. hadley commented on May 22, 2026

    @hadley
    Member

    I think I've changed my mind again — method() does already accept any class specification, the problem is that it requires generic to be an S7 generic. I would in principle be open to allowing generic to be an S3 or S4 generic, but the problem is (as far as I know) there's no existing base function that takes an S3 generic and an S3 object and returns the method that will be called. Implementing that feels out of scope for S7 (I've implemented this in sloop, and it's not simple).

  8. t-kalinowski commented on May 25, 2026

    @t-kalinowski
    Member

    One additional note: while in principle it's possible to get reasonably close to finding the S3 method with utils::getS3method() and .class2(), we'd still be left with the difficulty that S3 method lookup starts in the calling environment. The S3 methods list is not a stable value; it varies depending on the calling frame and other objects defined in the calling frame. That's in tension with how S7 generally behaves in other contexts, where method lookup is stable for a given generic and class signature. Supporting this faithfully would require expanding the signature to include the calling environment and accepting that method() can return unstable results.

  9. added a commit that references this issue on May 26, 2026
    5988ac7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureintrospection 🕵️‍♀️

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions