Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let child classes change default values of parent properties #473

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion R/constructor.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ constructor_args <- function(parent, properties = list()) {
if (is_class(parent) && !parent@abstract) {
# Remove any parent properties; can't use parent_args() since the constructor
# might automatically set some properties.
self_arg_nms <- setdiff(self_arg_nms, names2(parent@properties))
parent_prop_nms <- names2(parent@properties)
overridden <- intersect(self_arg_nms, parent_prop_nms)
for(name in overridden)
parent_args[[name]] <- prop_default(properties[[name]])

self_arg_nms <- setdiff(self_arg_nms, parent_prop_nms)
}

self_args <- as.pairlist(lapply(
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-constructor.R
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,19 @@ test_that("Dynamic settable properties are included in constructor", {
expect_equal(foo@dynamic_settable, 1)

})


test_that("Child classes can change property default value", {

Foo1 = new_class("Foo1", properties = list(
bar = class_vector
))

Foo2 = new_class("Foo2", Foo1, properties = list(
bar = new_property(default = 99)
))

expect_identical(formals(Foo1), pairlist(bar = logical()))
expect_identical(formals(Foo2), pairlist(bar = 99))

})
Loading