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
@t-kalinowski hmmm, that feels weird to me because dim is orthogonal to class in S3. So I would have expected something more like class_array_logical, class_array_numeric etc.
One could think of matrix and array as containers. Specifying the contents of a container is typically done through parameterization. Parameterized types would be interesting. Maybe they are represented by a higher order function that accepts the type parameters and returns a constructor that ensures the right mode?
If the object does not have a class attribute, it has an implicit class. Matrices and arrays have class "matrix" or "array", followed by the class of the underlying vector. Most vectors have a class based on the result of mode(x), except that integer vectors have the class c("integer", "numeric"), and real vectors have the class c("double", "numeric").
So, it seems we need to think of this in terms of "implicit" classes.
We currently handle the implicit "numeric" class using a union of class_integer and class_double.
Union classes cannot be used as a parent in new_class(parent=), but they can be used in method<- and in new_property(). This restriction sets a precedent: implicit class types cannot be used in all contexts.
One temptation is to open the barn doors and think through what it means to support arbitrary "implicit" classes, similar to a mixin class in Python or a Rust trait—where a generic can make assumptions about the object’s properties.
For example, in #433, we seem to be reaching for a class_scalar. One could imagine:
class_scalar<- new_class(
"class_scalar",
validator=function(self)
if (!identical(length(self), 1L))
"must be length 1"
)
Or, continuing along this desire path, one could further imagine implicit types as peers of regular types, allowed to be children of union type and composable with base types.
class_scalar<- new_class(
"class_scalar",
parent=class_vector,
validator=function(self)
if (!identical(length(self), 1L))
"must be length 1"
)
class_string<-
new_class("class_string", parent= c(class_character, class_scalar))
This would be straightforward to implement from the constructor's side. However, the challenge is method dispatch with base objects that lack a class attribute. For S7 to be viable, method dispatch must be efficient.
I see no efficient way to support:
Dispatch on arbitrary combinations of the class vector without an exponential increase in method search time.
Dispatch on additional "implicit" entries created at runtime by calling arbitrary R closures.
Another question to consider: What happens when a class_array is not explicitly declared but might be expected to appear implicitly?
Perhaps, we continue with the existing precedent and intentionally limit support for the concept of implicit classes in S7, beyond some minimal support in method<- for compatability with S3.
E.g., we offer no default constructor for class_numeric or class_POSIXt, and perhaps, neither for class_array and class_matrix.
> new_class("Foo", class_POSIXt)()
Error:!S3class<POSIXt>doesn't have a constructor> new_class("Foo", class_numeric)()Error:! `parent` must be an S7 class, S3 class, or base type, not an S7 union.
This could be via new_class("class_array", abstract = TRUE), or via some other, new mechanism.
Another idea is introducing a counterpart to new_union, called new_intersection. Like new_union, it wouldn't be usable in all contexts: The inverse of new_union(), it would valid in new_class() and new_property(), but not in method<-. An intersection could stack or combine validators (and obj_dispatch() entries), enabling use cases like this.
It seems like we should at least merge the existing implicit classes from class() at construction time and have a dim<-.S7_object() that updates it. And merge .class2() during dispatch if we are not already.
In the general case, I think we'll start running into issues similar to those of multiple inheritance. How do we linearize the hierarchy with these implicit/dynamic classes?
One way to make implicit classes semi-efficient would be to figure out the classes upon class definition and upon each modification of the object, to the extent possible, alongside validation.
Definitely worth a discussion at our next meeting.
It's worth noting that it's not valid to have a dim attribute on a non-vector.
library(S7)
Foo<- new_class("Foo", properties=list(
dim= new_property(NULL|class_integer)
))
foo<- Foo()
foo@dim<-1:3#> Error in `@<-.S7_object`(`*tmp*`, "dim", value = 1:3): invalid first argument, must be vector (list or atomic)
dim(foo) <-1:3#> Error in dim(foo) <- 1:3: invalid first argument, must be vector (list or atomic)
This error message comes from base R Rf_setAttrib() calling C function dimgets(), which has the check.
We have a fix in flight for the issues with a dim property in #673, so I think we can now ignore that wrinkle.
What if we treat class_matrix and class_array as unions? They could either be over class_double_matrix and class_integer_matrix (which I expect is what most people actually want), or over the full set of class_{atomic}_matrix if we want to keep the same semantics as S3.
One wrinkle I forgot about is that S3 can't define a method for (e.g.) "numeric matrices" so that would imply you can only register S3 methods for the new_matrix() union, not the individual elements. That feels a bit weird, but is probably an ok special case for backward compatibility.
Activity
TimTaylor commentedon Sep 6, 2024
sjcowtan commentedon Sep 9, 2024
t-kalinowski commentedon Oct 11, 2024
hadley commentedon Oct 11, 2024
@t-kalinowski how does that work? what type of array do you get?
t-kalinowski commentedon Oct 11, 2024
length 0 logical (this makes the most sense, for the same reason that
NAis logical; it can easily be promoted to another atomic)Created on 2024-10-11 with reprex v2.1.1
hadley commentedon Oct 11, 2024
@t-kalinowski hmmm, that feels weird to me because
dimis orthogonal to class in S3. So I would have expected something more likeclass_array_logical,class_array_numericetc.t-kalinowski commentedon Oct 11, 2024
I agree, this should be easy.
We only provide
class_*for actual base classes, that seems like a compound class.I wonder if we should introduce a
&/new_intersection()for classes, complimenting|/new_union(). Then,class_array_logicalcould be achieved with:hadley commentedon Oct 11, 2024
It feels like we should discuss this design a bit more before committing to it in a release. Maybe roll back for this release?
mmaechler commentedon Oct 15, 2024
This sounds nice to me ((without further deep thinking though) independent of
class_arrayetc.lawremi commentedon Oct 16, 2024
One could think of matrix and array as containers. Specifying the contents of a container is typically done through parameterization. Parameterized types would be interesting. Maybe they are represented by a higher order function that accepts the type parameters and returns a constructor that ensures the right mode?
The interface might look like:
Or, we more directly address the contract of the dim attribute presence using something like Haskell type classes:
But that will really slow down dispatch. The methods package did explore this approach with
setIs(test=)but it was never used for method selection.t-kalinowski commentedon Oct 16, 2024
Quoting from
?UseMethod:So, it seems we need to think of this in terms of "implicit" classes.
We currently handle the implicit "numeric" class using a union of
class_integerandclass_double.Union classes cannot be used as a parent in
new_class(parent=), but they can be used inmethod<-and innew_property(). This restriction sets a precedent: implicit class types cannot be used in all contexts.Some initial thoughts:
"array"and"matrix"are the only "implicit" S3 classes that are disconnected fromtypeof().One temptation is to open the barn doors and think through what it means to support arbitrary "implicit" classes, similar to a mixin class in Python or a Rust trait—where a generic can make assumptions about the object’s properties.
For example, in #433, we seem to be reaching for a
class_scalar. One could imagine:Or, continuing along this desire path, one could further imagine implicit types as peers of regular types, allowed to be children of union type and composable with base types.
This would be straightforward to implement from the constructor's side. However, the challenge is method dispatch with base objects that lack a
classattribute. For S7 to be viable, method dispatch must be efficient.I see no efficient way to support:
Another question to consider: What happens when a
class_arrayis not explicitly declared but might be expected to appear implicitly?There is already an inconsistency with the implicit "numeric" class, where we intentionally omit dispatching on
numeric.Perhaps, we continue with the existing precedent and intentionally limit support for the concept of implicit classes in S7, beyond some minimal support in
method<-for compatability with S3.E.g., we offer no default constructor for
class_numericorclass_POSIXt, and perhaps, neither forclass_arrayandclass_matrix.This could be via
new_class("class_array", abstract = TRUE), or via some other, new mechanism.Another idea is introducing a counterpart to
new_union, callednew_intersection. Likenew_union, it wouldn't be usable in all contexts: The inverse ofnew_union(), it would valid innew_class()andnew_property(), but not inmethod<-. An intersection could stack or combine validators (andobj_dispatch()entries), enabling use cases like this.lawremi commentedon Oct 16, 2024
It seems like we should at least merge the existing implicit classes from
class()at construction time and have adim<-.S7_object()that updates it. And merge.class2()during dispatch if we are not already.In the general case, I think we'll start running into issues similar to those of multiple inheritance. How do we linearize the hierarchy with these implicit/dynamic classes?
One way to make implicit classes semi-efficient would be to figure out the classes upon class definition and upon each modification of the object, to the extent possible, alongside validation.
Definitely worth a discussion at our next meeting.
t-kalinowski commentedon Oct 21, 2024
It's worth noting that it's not valid to have a
dimattribute on a non-vector.This error message comes from base R
Rf_setAttrib()calling C functiondimgets(), which has the check.lawremi commentedon Oct 25, 2024
That could be loosened to allow
OBJSXP. There would be additional constraints though, like having alength()method.hadley commentedon May 31, 2026
We have a fix in flight for the issues with a
dimproperty in #673, so I think we can now ignore that wrinkle.What if we treat
class_matrixandclass_arrayas unions? They could either be overclass_double_matrixandclass_integer_matrix(which I expect is what most people actually want), or over the full set ofclass_{atomic}_matrixif we want to keep the same semantics as S3.lawremi commentedon Jun 1, 2026
This is a pragmatic approach for the existing base types.
hadley commentedon Jun 1, 2026
One wrinkle I forgot about is that S3 can't define a method for (e.g.) "numeric matrices" so that would imply you can only register S3 methods for the
new_matrix()union, not the individual elements. That feels a bit weird, but is probably an ok special case for backward compatibility.