Proposal: allow S7 class inheritance merging and a "S3" nextMethod() dispatch approach.#566
Open
jtlandis wants to merge 2 commits intoRConsortium:mainfrom
Open
Proposal: allow S7 class inheritance merging and a "S3" nextMethod() dispatch approach.#566jtlandis wants to merge 2 commits intoRConsortium:mainfrom
nextMethod() dispatch approach.#566jtlandis wants to merge 2 commits intoRConsortium:mainfrom
Conversation
appropriate common parent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR provides a way for S7 objects to dynamically inherit from the same root class. For example, if one package “A” exports a popular S7 class, and is then extended directly by package “B” and “C”, this is currently possible. Specifically, B can be made from C, and C can be made from B (because they both are instances of A), but this causes them to loose all information relating to the original class.
For example, creating an instance of C through B will cause the resulting inheritance to look like
<C/A>with all properties relating toBbeing present as attributes, but not considered to be anything related to the class inheritance.This PR would make it so creating such instances, i.e. C from B, would result in an inheritance of
<C/B/A>in which properties ofBare preserved, provided that they do not interfere with properites defined in the explicit classC.Change 1
The main change is a single function
merge_S7_class(class, other), which does a few things:otheris an instance ofclass’s parent, and can thus be interchangable with the original parent.otherparent and those specified withclass.otherWith these changes, it is enough to create the intended behavior.
example of dynamic (or S3 like) inheritance
Change 2
The second change is the function
next_super(). This is to support method dispatch when the methods between our classCand some super methodAis unknown. This can thus allow packages to use a generic across the entire parent class heirarchy.Essentually, works the same as
superexcept we find the appropriate parent class from the object, as this may be the source of truth. Then ignore the first class fromclass_dispatch(), as the goal is to user the next available method. The below example shows its usecase.Created on 2025-09-13 with reprex v2.1.1