Repository navigation
Implement S7_user_frame(), S7_generic_call(), and S7_generic_fun() - #681
Conversation
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
t-kalinowski
left a comment
There was a problem hiding this comment.
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.
|
Ooh, good catch! |
| frame | ||
| } | ||
|
|
||
| # S7_dispatch() marks the generic's frame with `_dispatched_super` when it |
There was a problem hiding this comment.
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.
t-kalinowski
left a comment
There was a problem hiding this comment.
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.
|
@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 😆 |
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
|
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? |
|
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? |
# 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.
S7_user_frame() and S7_generic_call()S7_user_frame(), S7_generic_call(), and S7_generic_fun()
|
I’ve added I think this PR is mostly ready to merge. One question that might be worth revisiting is what the default value for |
|
What do you think of making it a enum, like 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. |
|
My thought behind making the default |
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.
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 viamatch = 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()andS7_user_frame()default toskip = "none", which keeps the nearest generic call and its caller's frame. Useskip = "super"to skip same-genericsuper()redispatches. The helpers aren't supported in methods for S3/S4 generics or operators.Internally,
S7_dispatch()recordssuper()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