Skip to content

Implement S7_user_frame(), S7_generic_call(), and S7_generic_fun() - #681

Merged
t-kalinowski merged 20 commits into
mainfrom
frame-helpers
Sep 30, 2026
Merged

t-kalinowski merged 20 commits into
mainfrom
frame-helpers

Conversation

@hadley

@hadley hadley commented Jun 4, 2026 •

Copy link
Copy Markdown
Member

Adds three helpers for use inside methods of S7 generics, including functions called by those methods:

  • S7_generic_call() returns the nearest call to the generic, with optional argument matching via match = TRUE.
  • S7_user_frame() returns the frame from which the generic was called, for non-standard evaluation.
  • S7_generic_fun() returns the generic function itself.

S7_generic_call() and S7_user_frame() default to skip = "none", which keeps the nearest generic call and its caller's frame. Use skip = "super" to skip same-generic super() redispatches. The helpers aren't supported in methods for S3/S4 generics or operators.

Internally, S7_dispatch() records super() redispatches in its own frame so the helpers can identify them without adding bindings to the generic's frame. Includes documentation, examples, and tests.

Fixes #596

hadley added 2 commits June 4, 2026 12:32
These are not exactly symmetric, but I think they solve the common cases, and the implementation is general enough that we can expand in the future if we discover the need.

Fixes #662
@hadley
hadley requested a review from t-kalinowski June 8, 2026 14:50

@t-kalinowski t-kalinowski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a failing regression test for this case: same-generic nested calls are not super redispatches.

Right now we can false positive in detecting super() redispatch for an ordinary nested call to the same generic, e.g. a method calling foo("inner"), currently gets treated like foo(super(...)) because the caller’s parent is the same generic. As a result, S7_generic_call() reports the outer call and S7_user_frame() returns the outer caller frame, instead of the inner generic call and the method frame that invoked it.

@hadley

hadley commented Jun 8, 2026

Copy link
Copy Markdown
Member Author

Ooh, good catch!

Comment thread R/method-call.R Outdated
frame
}

# S7_dispatch() marks the generic's frame with `_dispatched_super` when it

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I love this approach, but it feels safer than either trying to inspect the generic call for calls or super(), or evaluating each of the arguments to the generic to see if they have class S7_super.

@hadley
hadley requested a review from t-kalinowski June 10, 2026 13:17

@t-kalinowski t-kalinowski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defining a sentinel value in the generic frame seems fine for now, until a more elegant solution emerges. However, with the current implementation, we walk past all super frames, even those for another method. I pushed a test (currently failing) covering the case of a method calling another method with super.

@hadley

hadley commented Jun 10, 2026

Copy link
Copy Markdown
Member Author

@t-kalinowski do you want to just finish this off? It seems a bit pointless for you to make a failing test and then for me to tell claude to fix it 😆

t-kalinowski and others added 2 commits June 11, 2026 11:32
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@t-kalinowski

Copy link
Copy Markdown
Member

Thanks! Happy to. I pushed a change. Github won't let me request a review from the original author (you). Can you please take a look?

@hadley

hadley commented Jun 20, 2026

Copy link
Copy Markdown
Member Author

I'm worried that the complexity here is a sign that the idea to go all the way back to the originating generic call is a bad idea. But it still seems useful, so what do you think about gating it behind an argument? So by default these functions return the immediate generic frame, but you can opt-in to navigating all the way back past any super calls?

@hadley hadley mentioned this pull request Jun 20, 2026
# Conflicts:
#	NAMESPACE
#	_pkgdown.yml
S7_dispatch() evaluates the method in the generic's frame, so the generic
is always the method's direct parent. Walking further up the parent chain
let methods invoked another way (an operator method, or a method called
directly) report an unrelated enclosing generic. They now error instead.

Also document that the helpers work from functions called by a method,
and that methods for S3/S4 generics and operators aren't supported.
Use `:=` for named generics and classes, and snapshot errors instead of
matching them with expect_error().
Both helpers still skip same-generic super() re-dispatches by default,
which is what error messages and non-standard evaluation want. Setting
`skip_super = FALSE` stops at the nearest call to the generic instead.

S7_generic_fun() doesn't take the argument: super() only re-dispatches
the same generic, so the result is the same either way.
@t-kalinowski t-kalinowski changed the title Impement S7_user_frame() and S7_generic_call() Implement S7_user_frame(), S7_generic_call(), and S7_generic_fun() Sep 29, 2026
@t-kalinowski

Copy link
Copy Markdown
Member

I’ve added skip_super to S7_generic_call() and S7_user_frame(). It currently defaults to TRUE, so both helpers skip past same-generic super() calls.

I think this PR is mostly ready to merge. One question that might be worth revisiting is what the default value for skip_super should be. Currently it is TRUE, but I welcome discussion about whether it should be FALSE.

@hadley

hadley commented Sep 29, 2026

Copy link
Copy Markdown
Member Author

What do you think of making it a enum, like skip = c("none", "super").

I feel like we should default to none because that's safer, but maybe you've done enough with this code to feel confident that it's correct now.

@t-kalinowski

Copy link
Copy Markdown
Member

My thought behind making the default TRUE is that most people using super are probably going to be advanced users working in the context of a package. So, in most scenarios, the super call probably won’t be the most relevant call for end users. This is all speculative, of course. I'll chage it so by default we don't skip the super call.

Replace skip_super with skip = c("none", "super"), defaulting to the
nearest generic call and caller frame. Update tests, documentation,
examples, and NEWS for the new API.
Store the marker in the dispatch wrapper's frame and locate it by the
dispatch function and its generic parent. Cover marker leakage, local
binding collisions, and false positives from same-named generic locals.
Follow the frame adjacency already required by S7_dispatch(), retaining
the function-identity check. Add a regression for two dispatches in one
generic invocation so an earlier super call cannot affect a later call.
@t-kalinowski
t-kalinowski merged commit 6004091 into main Sep 30, 2026
15 checks passed
@t-kalinowski
t-kalinowski deleted the frame-helpers branch September 30, 2026 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants