Skip to content

Generics are embedded when defining methods #364

Closed

Description

@hadley
library(S7)
foo1 <- new_class("foo1", properties = list(x = class_integer))
foo2 <- new_class("foo2", foo1, properties = list(y = class_double))

method(convert, list(foo1, class_integer)) <- function(from, to) {
  from@x
}
ls()
#> [1] "convert" "foo1"    "foo2"

I think this is going to be a problem because defining a method for a generic defined in another package will end up embedding a copy of that generic in your package. Not sure what we can do here given that this is a fairly deep seated behaviour of <- functions.

Activity

  1. hadley commented on Sep 16, 2023

    @hadley
    MemberAuthor

    Oh maybe method<- could just return NULL? — Nope that doesn't work because if the generic is defined in the current environment, it will reset it.

    This almost works:

      if (identical(environment(generic), parent.frame())) {
        invisible(generic)
      } else {
        invisible(NULL)
      }

    But isn't quite right either.

    Maybe we need to do some post-hoc clean up?

  2. hadley commented on Oct 16, 2023

    @hadley
    MemberAuthor

    Something like this would work:

    #' .onLoad <- function(...) {
    #'   S7::methods_register()
    #' }
    #' S7::package_clean_up()
  3. hadley commented on Oct 16, 2023

    @hadley
    MemberAuthor

    Maybe if generic is an external generic, return that object instead of the actual generic. Do something to these external generic objects on load.

  4. added this to the v0.2.0 milestone on Oct 17, 2023
  5. hadley commented on Nov 27, 2023

    @hadley
    MemberAuthor

    Maybe this isn't as hard as I thought — method_register() already has all the mechanics to return an external generic if needed, and there's no harm in that object continuing to live in the package namespace. (And in fact there's a small advantage in that we only need to create the external generic object once, rather than once each time).

  6. added a commit that references this issue on Nov 27, 2023
    21bcd2d
  7. modified the milestones: v0.2.0, v0.3.0 on Nov 5, 2024
  8. added
    bug
    on May 21, 2026
  9. changed the title [-]Embedding generics when defining methods[/-] [+]Generic are embedded when defining methods[/+] on May 21, 2026
  10. changed the title [-]Generic are embedded when defining methods[/-] [+]Generics are embedded when defining methods[/+] on May 21, 2026
  11. hadley commented on May 22, 2026

    @hadley
    MemberAuthor

    #390 (comment) suggests that the mere presence of an object (not a function) is sufficient to trigger some of the problems. And since package bindings are locked by the time we get to .onLoad() we can't just assign a sentinel and then later remove it in register_methods().

  12. added a commit that references this issue on Jun 9, 2026
    6545fa8
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

    bugregistration 📓

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions