Skip to content

Draft: Reimplement object construction - #1

Open
dgkf wants to merge 5 commits into
mainfrom
prop-masking
Open

dgkf wants to merge 5 commits into
mainfrom
prop-masking

Conversation

@dgkf

@dgkf dgkf commented Dec 17, 2025 •

Copy link
Copy Markdown
Owner

opening this PR only to document code changes

Design goals

  • objects are constructed incrementally from parents, retaining current behavior that incrementally builds an object and applies parent validators and setters where applicable.
  • object properties can be redefined in child classes
  • custom constructors are incrementally applied, allowing for custom constructors at each stage of the object construction.

Breaking changes

  • Two of the more exotic custom constructor behaviors in the test suite now break. This is an unavoidable consequence of allowing properties to be passed to both parent constructors while also being able to be masked by child constructors. Annotations inline below.

Comment thread R/class.R
attr(object, "properties") <- all_props
attr(object, "abstract") <- abstract
attr(object, "constructor") <- constructor
attr(object, ".constructor_is_default") <- constructor_is_default

@dgkf dgkf Dec 17, 2025 •

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Comment thread R/class.R
Comment on lines +292 to 298
# instantiate with most recent custom constructor
idx <- Position(
Negate(uses_default_constructor),
# ignore current class constructor
class_lineage[-1L],
nomatch = 0L
)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

We seek over the classes in our class lineage to find which class to start from during construction

Comment thread R/class.R
Comment on lines +303 to +309
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]]

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

We match our arguments against that constructor and propagate any applicable arguments to the constructor to first create our object.

Comment thread R/class.R
Comment on lines +311 to 331
# 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
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Comment thread R/constructor.R
}

if (identical(parent, S7_object) || (is_class(parent) && parent@abstract)) {
new_object_call <- new_call(obj_call, self_args)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Comment on lines 171 to +172
Output
A B C
C B A

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

snapshot test diff

An example of how the constructors change. Many snapshots change similarly.

Comment on lines 485 to 487
), constructor = function(x) {
new_object(substitute(x))
new_object(.data = substitute(x))
})

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Comment on lines -93 to 104
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())

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

This branch has not been deployed

No deployments
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.

1 participant