Skip to content

Implement external classes#660

Open
hadley wants to merge 20 commits into
mainfrom
external-class
Open

Implement external classes#660
hadley wants to merge 20 commits into
mainfrom
external-class

Conversation

@hadley

@hadley hadley commented May 28, 2026

Copy link
Copy Markdown
Member

I think the use of external is a little bit misleading here, because these are really "delayed" classes. But external matches external generic, which exist for a similar reason.

Fixes #250. Fixes #573.

Note this does not solve #317, since currently creating a subclass will force loading of the package via class_constructor(). But I think can fix that after #694 by using ... to call constructors from other packages. That will decouple the superclass arguments from the subclass in a way that won't cause R CMD check to fail with a codoc mismatch if the parent args change.

hadley added 2 commits June 10, 2026 08:19
# Conflicts:
#	NEWS.md
#	R/class-spec.R
#	R/external-generic.R
#	_pkgdown.yml
#	tests/testthat/_snaps/property.md
@hadley hadley marked this pull request as ready for review June 10, 2026 17:16
@hadley hadley requested review from lawremi and t-kalinowski and removed request for t-kalinowski June 10, 2026 17:17
@hadley

hadley commented Jun 10, 2026

Copy link
Copy Markdown
Member Author

@t-kalinowski @lawremi I think this is now ready for your feedback.

@lawremi lawremi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a very useful feature. Implementation looks good.

@hadley hadley requested a review from t-kalinowski June 16, 2026 12:11
hadley and others added 7 commits June 16, 2026 07:17
Add regression tests for method registration outside packages, external
classes nested in union signatures, and unresolved external class
introspection.
Cover resolving external classes whose S7 name differs from their namespace
binding, preserving generic sentinels for deferred foreign methods, and rejecting
unprovable property overrides involving unresolved external classes.
Resolve external classes by S7 class name, preserve generic sentinels when
deferring methods for foreign generics with external-class signatures, and avoid
treating unresolved external classes as an empty inheritance hierarchy.
Comment thread R/external-class.R
Comment on lines +172 to +178
(
identical(S7_class_name(obj), x$class_name) ||
(
identical(obj@name, x$name) &&
(is.null(obj@package) || identical(obj@package, x$package))
)
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[air] reported by reviewdog 🐶

Suggested change
(
identical(S7_class_name(obj), x$class_name) ||
(
identical(obj@name, x$name) &&
(is.null(obj@package) || identical(obj@package, x$package))
)
)
(identical(S7_class_name(obj), x$class_name) ||
(identical(obj@name, x$name) &&
(is.null(obj@package) || identical(obj@package, x$package))))

@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 tests and fixes for some edge cases. Can you please take a look and see if the approach makes sense?

An open question is what behavior we want for property override checks when an external class is from a package that is not loaded yet. The current fix avoids treating an unresolved external class as matching every child type, but it's possible we want to take a different approach here (e.g., defer until the package is loaded, or load the package eagerly)

@hadley hadley mentioned this pull request Jun 17, 2026
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.

Defining generic methods for suggested packages How to create a nestable R7 class?

3 participants