From a0523436b7b0d2fbc16cbb9be199c58ccf53bed5 Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Mon, 21 Oct 2024 12:19:55 -0400 Subject: [PATCH 1/2] Allow child classes to change default value of parent properties closes #467 --- R/constructor.R | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/R/constructor.R b/R/constructor.R index 63df1e78..738555df 100644 --- a/R/constructor.R +++ b/R/constructor.R @@ -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( From 5c072c8d645ec11509d961dabc1ee34e580986b4 Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Mon, 21 Oct 2024 12:20:08 -0400 Subject: [PATCH 2/2] add tests --- tests/testthat/test-constructor.R | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/testthat/test-constructor.R b/tests/testthat/test-constructor.R index 02152dc3..3b744885 100644 --- a/tests/testthat/test-constructor.R +++ b/tests/testthat/test-constructor.R @@ -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)) + +})