You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have originally created #566 but I did not provide a motivating example for this pattern. Specifically, in S7, If two subclasses do not interfere with each other, how can they extend the same root object at the same time?
Suppose for example, 2 packages both extend ggplot2::ggplot, but neither property that they contribute interferes with the other package. This would theoretically allow both packages to build their subclass from the other. However considering how the S7_class attribute is added to the object, this enforces a fairly strict inheritance pattern in that if class C builds itself from class B, it will loose all properties from the B class.
even though B is constructed first and would get the integer vector, that attribute is stripped
attr(C(B(gg)), "bprop")
# NULL
Is the expected form for developers to eagerly transform classes with S7::convert?
What if during dispatch we just want to yield to the next logical class with NextMethod()?
Even if we manually manipulate the class field, C(B(gg)) will not have the class B within the parent chain.
I believe multiple inheritance is slightly different than what I am describing. I'd imagine multiple inheritance would be implemented on S7_unions, which I understand that new_class objects cannot use as inheritance.
I'm wondering how feasible would it be for the new_object constructor to not ignore the S7 class provided. In the example below, the instance of "B" is still an instance of class_ggplot.
Also, it appears I have a bug in my above code, new_object doesn't touch other attributes provided - so in some capacity, the object retains attribute data.
So in the above you can retain some data, so long as you are careful on how you change between objects, but as far as the dispatch system is aware, B -> class_ggplot and C -> class_ggplot, and there is no world in which B -> C -> class_ggplot or C -> B -> class_ggplot.
If S7 inheritance should remain this strict, then we can go ahead and close this.
Putting aside the confusion around inheritance (sorry @jtlandis), this is exposing a limitation in S7 classes that inherit from S3 classes. Since we don't know the attributes carried by the S3 ggplot objects, we can't strip attributes during construction, so foreign attributes are being carried around on the instances. If S7 supported defining the set of allowed attributes on an S3 class, like how the methods package does via setOldClass(, S4Class=dummy_class_with_attributes), then we could strip the invalid attributes at construction time.
I see, it's my understanding now that if S7 knew all possible S3 attributes, it would strip unknown attributes.
So the current new_object function keeps old attributes as a side effect of this limitation?
And I suppose by extension, S7 inheritance is supposed to be stricter than it is?
Just another thing to possibly consider in terms of S7 usage patterns. It seems the project is establishing different expectations for a new class' relationship to other types with respects to inheritance and composition.
Specifically, inheritance is becoming more strict and linear. However, when it comes to composition, i.e. the defined classes for data properties on the object, the property will accept any subclass object without strictly coercing it.
Given how properties accepted data, I thought it would be logical for class inheritance to do the same. I rather like the flexibility of accepting subclasses, hence why I wrote my earlier PR and this issue.
However, if inheritance does become more strict, I would hope that the current behavior for setting data properties (i.e. subclasses are acceptable by default) will remain.
I think this is more a question about constructor semantics. We've long ago established that a constructor should return an instance of its class, not an instance of a subclass. The constructor checks the validity of the object, including whether each property's value inherits from the property's class, but there is no coercion of property values, unless the class does something on its own via a custom setter. This is consistent with S4 and every class-based OOP system I can think of.
I'm curious about your use case for loosening the constructor contract.
The specifics was inspired by a theoretical interaction with my package ggside and another package gganimate. When S7 was coming down the pipeline for ggplot2, I noticed that using nextMethod() semantics for S3 dispatch was no longer possible, as you needed to the exact class to dispatch to, and S7 constructors do not retain subclasses. ggside was written to be a light wrapper around ggplot2, mostly just touching how faceting worked, so it was fairly compatible with packages that had their own ggplot_build method, by simply deferring to use the next S3 subclass (ggside previously just prepended its class to class attribute).
In short, most of this issue and that PR was an attempt to re-introduce S3 flexibility to S7.
FWIW, strict subclassing complicates compatibility between two packages that embrace S7 subclassing of some core package. Continuing to use ggplot2 as the example, each may establish their + method that either create their class instance, or does some book-keeping on the state of the object. If subclassing is inflexible then one of the following is true about the two packages:
The two packages are fundamentally incompatible.
They must define subclasses of each other to be compatible.
They don't define S7 instances, and modify class directly by prepending the class attribute.
This is a very specific example for ggplot2, so I don't necessarily believe that the S7 project should change to accommodate ggplot2.
That being said, I am unsure what the current state of the interaction between ggside and gganimate is, as the original PR was opened over a year ago. Last I checked, gganimate is not using S7 instances yet. I have likely made changes to ggside in the meantime and may take different approaches to how it works in the future, considering if I have time to do so.
Activity
hadley commentedon Oct 2, 2026
It sounds like you are asking for multiple inheritance, which S7 doesn't support.
jtlandis commentedon Oct 2, 2026
I believe multiple inheritance is slightly different than what I am describing. I'd imagine multiple inheritance would be implemented on S7_unions, which I understand that
new_classobjects cannot use as inheritance.I'm wondering how feasible would it be for the
new_objectconstructor to not ignore the S7 class provided. In the example below, the instance of "B" is still an instance of class_ggplot.Also, it appears I have a bug in my above code,
new_objectdoesn't touch other attributes provided - so in some capacity, the object retains attribute data.So in the above you can retain some data, so long as you are careful on how you change between objects, but as far as the dispatch system is aware,
B -> class_ggplotandC -> class_ggplot, and there is no world in whichB -> C -> class_ggplotorC -> B -> class_ggplot.If S7 inheritance should remain this strict, then we can go ahead and close this.
lawremi commentedon Oct 2, 2026
Putting aside the confusion around inheritance (sorry @jtlandis), this is exposing a limitation in S7 classes that inherit from S3 classes. Since we don't know the attributes carried by the S3 ggplot objects, we can't strip attributes during construction, so foreign attributes are being carried around on the instances. If S7 supported defining the set of allowed attributes on an S3 class, like how the methods package does via
setOldClass(, S4Class=dummy_class_with_attributes), then we could strip the invalid attributes at construction time.jtlandis commentedon Oct 2, 2026
I see, it's my understanding now that if S7 knew all possible S3 attributes, it would strip unknown attributes.
So the current new_object function keeps old attributes as a side effect of this limitation?
And I suppose by extension, S7 inheritance is supposed to be stricter than it is?
jtlandis commentedon Oct 6, 2026
Just another thing to possibly consider in terms of S7 usage patterns. It seems the project is establishing different expectations for a new class' relationship to other types with respects to inheritance and composition.
Specifically, inheritance is becoming more strict and linear. However, when it comes to composition, i.e. the defined classes for data properties on the object, the property will accept any subclass object without strictly coercing it.
Given how properties accepted data, I thought it would be logical for class inheritance to do the same. I rather like the flexibility of accepting subclasses, hence why I wrote my earlier PR and this issue.
However, if inheritance does become more strict, I would hope that the current behavior for setting data properties (i.e. subclasses are acceptable by default) will remain.
lawremi commentedon Oct 6, 2026
I think this is more a question about constructor semantics. We've long ago established that a constructor should return an instance of its class, not an instance of a subclass. The constructor checks the validity of the object, including whether each property's value inherits from the property's class, but there is no coercion of property values, unless the class does something on its own via a custom setter. This is consistent with S4 and every class-based OOP system I can think of.
I'm curious about your use case for loosening the constructor contract.
jtlandis commentedon Oct 9, 2026
The specifics was inspired by a theoretical interaction with my package
ggsideand another packagegganimate. When S7 was coming down the pipeline forggplot2, I noticed that usingnextMethod()semantics for S3 dispatch was no longer possible, as you needed to the exact class to dispatch to, and S7 constructors do not retain subclasses.ggsidewas written to be a light wrapper aroundggplot2, mostly just touching how faceting worked, so it was fairly compatible with packages that had their own ggplot_build method, by simply deferring to use the next S3 subclass (ggsidepreviously just prepended its class to class attribute).In short, most of this issue and that PR was an attempt to re-introduce S3 flexibility to S7.
FWIW, strict subclassing complicates compatibility between two packages that embrace S7 subclassing of some core package. Continuing to use
ggplot2as the example, each may establish their+method that either create their class instance, or does some book-keeping on the state of the object. If subclassing is inflexible then one of the following is true about the two packages:This is a very specific example for
ggplot2, so I don't necessarily believe that the S7 project should change to accommodateggplot2.That being said, I am unsure what the current state of the interaction between
ggsideandgganimateis, as the original PR was opened over a year ago. Last I checked,gganimateis not using S7 instances yet. I have likely made changes toggsidein the meantime and may take different approaches to how it works in the future, considering if I have time to do so.