Repository navigation
Strip external generics on build - #662
Conversation
|
Might want to clarify in the docs that it should come after all method registration. The example in This also raises the idea of a combined function |
And refactor out some package helpers
|
Hmmm, I wonder if we can call ... Nope, that doesn't work. |
|
@t-kalinowski the actual implementation is straightforward, but I added quite a bit of test infrastructure to reduce duplication. Let me know if you want that in a separate PR. |
| } | ||
|
|
||
| generic_sentinel <- function(generic) { | ||
| external <- as_external_generic(generic) |
There was a problem hiding this comment.
I think this makes it so that an external S3 generic is not callable after a method is registered.
I think it's fine, since it's rare to want to call a generic at pkg build time.
However, we can also maybe just do:
attr(generic, "S7_external_generic_sentinel") <- TRUEand keep the generic as-is.
There was a problem hiding this comment.
Well, it binds a non-function object to the name in the package environment. But I think you'll still be able to call it, you'll just be calling the original version, not the version copied into the package.
(I had claude attempt to keep it callable, but because of the overlap in the existing external generic pathway, the code ends up being quite complicated).
| @@ -0,0 +1,4 @@ | |||
| export(t3_s3) | |||
| export(t3_s4) | |||
| exportMethods(t3_s4) | |||
There was a problem hiding this comment.
It would be good to expand this test to also register a method for a base R generic (e.g., sum, +).
…()` (#681) * Impement `S7_user_frame()` and `S7_generic_call()` 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 * Add a `match` argument * test: cover same-generic calls that are not super dispatches * Check for presence of `super()` * Use a sentinel marker for safety * test: cover super() passed to another generic * Fix method-call helper frame detection * Update R/method-call.R Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Add `S7_generic_fun()` * Require a method's generic to be its direct parent frame 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. * Follow current conventions in S7_generic_call() tests and examples Use `:=` for named generics and classes, and snapshot errors instead of matching them with expect_error(). * Clarify how S3/S4-registered methods interact with frame lookup * Add `skip_super` argument to S7_generic_call() and S7_user_frame() 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. * Make skipping super calls opt-in for frame helpers 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. * Keep frame helper NEWS focused on the new functions * Keep super dispatch markers in S7_dispatch frames 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. * Use the adjacent dispatch frame for super markers 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. --------- Co-authored-by: Tomasz Kalinowski <[email protected]> Co-authored-by: Tomasz Kalinowski <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Fixes #364