Skip to content

Strip external generics on build - #662

Merged
hadley merged 8 commits into
mainfrom
strip-external-generics
Jun 8, 2026
Merged

hadley merged 8 commits into
mainfrom
strip-external-generics

Conversation

@hadley

@hadley hadley commented May 28, 2026

Copy link
Copy Markdown
Member

Fixes #364

@lawremi

lawremi commented May 29, 2026

Copy link
Copy Markdown
Collaborator

Might want to clarify in the docs that it should come after all method registration. The example in zzz.R shows that, but it might help to be explicit.

This also raises the idea of a combined function S7::load() that clears generics and defines .onLoad() via side-effect. Probably not a very good idea, because confusion could result from another .onLoad() being defined. I wonder if the namespace system could support .onLoad.<foo> so that there can be multiple hooks.

And refactor out some package helpers
Comment thread tests/testthat/t4/R/t4.R Resolved
@hadley

hadley commented May 29, 2026 •

Copy link
Copy Markdown
Member Author

Hmmm, I wonder if we can call setHook(packageEvent(current_package, "onLoad")) to register an onLoad hook in S7_on_build()?

...

Nope, that doesn't work.

@hadley
hadley requested a review from t-kalinowski June 3, 2026 15:31
@hadley

hadley commented Jun 3, 2026

Copy link
Copy Markdown
Member Author

@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.

Comment thread R/hooks.R
}

generic_sentinel <- function(generic) {
external <- as_external_generic(generic)

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 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") <- TRUE

and keep the generic as-is.

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.

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)

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.

It would be good to expand this test to also register a method for a base R generic (e.g., sum, +).

@hadley
hadley merged commit 6545fa8 into main Jun 8, 2026
13 checks passed
@hadley
hadley deleted the strip-external-generics branch June 8, 2026 15:22
t-kalinowski added a commit that referenced this pull request Sep 30, 2026
…()` (#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>
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.

Generics are embedded when defining methods

3 participants