Skip to content

Should parent.frame() in S7 methods match S3? #596

Closed

Description

@t-kalinowski

Spun out from #592.

parent.frame() inside an S7 method currently differs from S3:

library(S7)

foo <- new_generic("foo", "x")
method(foo, class_any) <- function(x) parent.frame()

bar <- function(x) UseMethod("bar")
bar.default <- function(x) parent.frame()

foo(1)
#> <environment: 0x117954580>
bar(1)
#> <environment: R_GlobalEnv>

It would be nice if parent.frame() in an S7 method matched S3 here.

Activity

  1. hadley commented on May 21, 2026

    @hadley
    Member

    We should discuss this. I think there's some disagreement about how much we want to elide the details of method dispatch. I think I'd prefer to keep it explicit (as long as it's consistent, so that parent.frame(2) would always get you the caller of the generic).

  2. lawremi commented on May 22, 2026

    @lawremi
    Collaborator

    To clarify, it's consistency with both S3 and S4.

    I agree that it's confusing when the return value of sys.parents() repeats values (and parent.frame() skips frames):

    Browse[1]> sys.parents()
    [1] 0 0
    Browse[1]> where
    where 1 at #1: bar.default(1)
    where 2: bar(1)

    The at #1 is not intuitive and is easy to miss.

    Of course with the current S7 implementation, the same complexity exists, and there isn't even an indicator in the trace:

    Browse[1]> sys.parents()
    [1] 0 1 1
    Browse[1]> where
    where 1: `method(foo, class_any)`(x = 1, ...)
    where 2: S7::S7_dispatch()
    where 3: foo(1)

    One direction would be to improve the clarity of the traces, so that the user understands what is happening.

  3. hadley commented on Jun 1, 2026

    @hadley
    Member

    I wonder if we should expose two helpers for what you'd typically use parent.frame() — getting the frame of the generic (for error messages) and getting the user frame (for various NSE shenanigans). If we provided these helpers we could ensure that they always worked even when super() was in the mix.

    That would also free us to consider alternative implementation in the future.

  4. lawremi commented on Jun 2, 2026

    @lawremi
    Collaborator

    Yes, those helpers be.. helpful.. regardless of what we do with this issue. Abstraction is a good idea here.

  5. hadley commented on Jun 3, 2026

    @hadley
    Member

    Maybe S7_generic_frame() and S7_user_frame()?

  6. lawremi commented on Jun 3, 2026

    @lawremi
    Collaborator

    Seems reasonable.

  7. ltierney commented on Sep 30, 2026

    @ltierney
    Collaborator

    Does this mean S7 can now stop using R_findVarInFrame?

  8. t-kalinowski commented on Sep 30, 2026

    @t-kalinowski
    MemberAuthor

    Not yet. The dispatcher still uses Rf_findVarInFrame() to forward argument promises, preserving substitute(), missing(), and lazy evaluation. That remains tracked in #592. The new context helpers give us room to change the dispatch implementation, but they don’t themselves replace the argument-forwarding machinery.

  9. ltierney commented on Oct 2, 2026

    @ltierney
    Collaborator

    Not yet. The dispatcher still uses Rf_findVarInFrame() to forward argument promises, preserving substitute(), missing(), and lazy evaluation. That remains tracked in #592. The new context helpers give us room to change the dispatch implementation, but they don’t themselves replace the argument-forwarding machinery.

    Thanks. I'll try to get back to this in the next month or two.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions