You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
S7::method does not accept an S3 generic while the doc suggest it should. #568
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.
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.
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.
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).
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.
Activity
kevinushey commentedon Dec 8, 2025
+1. It's also awkward that one can define an S3 method in this way:
And yet you cannot retrieve it using the same syntax:
mmaechler commentedon Dec 18, 2025
method<-is defined inS7and 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
methodin S7 (and not in base, I was confusing it withmethods()),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.
hadley commentedon May 21, 2026
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.hadley commentedon May 21, 2026
Looking at the totality of other issues, I now believe we should just implement this.
hadley commentedon May 22, 2026
In general, it should accept any class specification.
hadley commentedon May 22, 2026
I think I've changed my mind again —
method()does already accept any class specification, the problem is that it requiresgenericto be an S7 generic. I would in principle be open to allowinggenericto 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).t-kalinowski commentedon May 25, 2026
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 thatmethod()can return unstable results.Clarify that `method()` only works with S7 generics (#617)