Repository navigation
Conversation
| attr(object, "properties") <- all_props | ||
| attr(object, "abstract") <- abstract | ||
| attr(object, "constructor") <- constructor | ||
| attr(object, ".constructor_is_default") <- constructor_is_default |
There was a problem hiding this comment.
Added a new attribute to the S7 object.
This flag helps us determine whether a custom constructor was provided for the class. If an object has a custom constructor in its class hierarchy, the most recent constructor is used as the starting point for building the object.
We want to build the object in its entirety first and only validate the object once the child-most object is created, avoiding recursing through constructors that each apply their own validation. There are certainly other ways to achieve this -- this was just one way that was rather immediately actionable with minimal changes.
| # instantiate with most recent custom constructor | ||
| idx <- Position( | ||
| Negate(uses_default_constructor), | ||
| # ignore current class constructor | ||
| class_lineage[-1L], | ||
| nomatch = 0L | ||
| ) |
There was a problem hiding this comment.
We seek over the classes in our class lineage to find which class to start from during construction
| constructor <- class_constructor(class_lineage[[idx]]) | ||
| cons_named_arg <- arg_names %in% formalArgs(constructor) | ||
| cons_accepts_ellipses <- "..." %in% formalArgs(constructor) | ||
| cons_unnamed_arg <- cons_accepts_ellipses & "" == arg_names | ||
| cons_args <- args[cons_named_arg | cons_unnamed_arg] | ||
| object <- do.call(constructor, cons_args, quote = TRUE) | ||
| attr(object, "S7_class") <- class_lineage[[idx]] |
There was a problem hiding this comment.
We match our arguments against that constructor and propagate any applicable arguments to the constructor to first create our object.
| # iterate over remaining classes and apply properties | ||
| for (class_i in rev(utils::head(class_lineage, idx - 1L))) { | ||
| if (class_i@abstract) { | ||
| next | ||
| } | ||
|
|
||
| attr(object, "class") <- class_dispatch(class_i) | ||
| attr(object, "S7_class") <- class_i | ||
|
|
||
| # Don't need to validate if parent class already validated, | ||
| # i.e. it's a non-abstract S7 class | ||
| parent_validated <- inherits(class@parent, "S7_object") && !class@parent@abstract | ||
| validate(object, recursive = !parent_validated) | ||
| # apply properties | ||
| prop_names <- intersect(arg_names, names(class_i@properties)) | ||
| if (S7_inherits(object)) { | ||
| props(object, check = FALSE) <- args[prop_names] | ||
| } else { | ||
| attributes(object)[prop_names] <- args[prop_names] | ||
| } | ||
| } | ||
|
|
||
| validate(object) | ||
| object | ||
| } |
There was a problem hiding this comment.
Then we build our class by iterating through the remainder of the class hierarchy, updating any properties that are applicable at each stage.
This could be more clever in situations where a class does not implement any setters, allowing us to leap a few classes in the class hierarchy. Once a stage in the hierarchy implements a setter, all bets are off and we have to assume that any property could be affected.
| } | ||
|
|
||
| if (identical(parent, S7_object) || (is_class(parent) && parent@abstract)) { | ||
| new_object_call <- new_call(obj_call, self_args) |
There was a problem hiding this comment.
To allow us to instantiate objects by implementing our own property masking upon creation, we need to change the way that the default constructor works.
function(a = "one", b = TRUE, c = 3L) {
- new_object(Parent(a = a, b = b), c = c)
+ new_object(a = a, b = b, c = c)
}Before, by immediately handing off any properties of the parent to the parent constructor, we have no ability to intercept those properties and handle property masking.
| Output | ||
| A B C | ||
| C B A |
There was a problem hiding this comment.
snapshot test diff
The order of validation gets flipped, and this is desirable. If an object fails to validate for a constraint applied to the parent class, we want to fail early in such situations because they are more basal constraints on the object.
Otherwise, all validators continue to be tested exactly once, which is the primary purpose of this test.
| x | ||
| y | ||
| new_object(S7_object(), x = x, y = y) | ||
| new_object(x = x, y = y) |
There was a problem hiding this comment.
snapshot test diff
An example of how the constructors change. Many snapshots change similarly.
| ), constructor = function(x) { | ||
| new_object(substitute(x)) | ||
| new_object(.data = substitute(x)) | ||
| }) |
There was a problem hiding this comment.
Breaking change
With the changes to the constructor, properties must be named unless the constructor accepts ....
Without this constraint, there is no way to tell for which properties the argument is intended and we'd attempt to assign it to the first property at each stage of object creation.
| bar <- new_class("bar", foo, properties = list(y = class_double)) | ||
| bar <- new_class( | ||
| "bar", | ||
| foo, | ||
| properties = list(y = class_double), | ||
| constructor = function(..., y = double()) { | ||
| new_object(x = list(...), y = y) | ||
| } | ||
| ) | ||
|
|
||
| expect_equal(bar()@x, list()) | ||
| expect_equal(bar(2)@x, list(2)) | ||
| expect_equal(bar(y = 2)@x, list()) |
There was a problem hiding this comment.
Breaking change
This use case is probably the biggest downside to this implementation, but I think it is unavoidable. Previously, when all constructor arguments that match a parent class are assumed to only be used by the parent, we can steer all the ellipsis arguments to that constructor.
This is because the default constructor of bar() will have the header function(..., x = list(), y = double()), automatically assigning a value for x which knows nothing about how the parent wants to handle ellipses.
If we wanted to handle this case, I think we should not provide default values in the constructor and should instead interpolate default values during construction. but this would be a more substantive change.
In that model, bar() would have the header function(..., x, y). When the parent is called it would accept any non-missing arguments that match its constructor signature (omitting x) and allow it to collect ... into an x list. Only when the object is constructed would be fall back to the default property value.
This would also make it far easier to write constructors without knowledge of S7 property default values.
opening this PR only to document code changes
Design goals
Breaking changes