Skip to content

Commit b730736

Browse files
committed
Make Variable@dims <- NULL reset to scalar
The dims setter early-returned on empty input, so resetting a variable to scalar (NULL dims, per the class's own convention) silently kept the stale dims. r2f-assign.R's deferred-mode path assigns `var@dims <- value@value@dims` with a legitimately-NULL RHS and relied on this working. Assign the attribute directly (the same S7 workaround already used by the `r` property) to avoid recursing through the setter.
1 parent c2ac3f0 commit b730736

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

R/classes.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ Variable := new_class(
240240
NULL | class_list,
241241
setter = function(self, value) {
242242
if (!length(value)) {
243+
# reset to scalar (NULL means scalar); assign the attribute
244+
# directly to avoid recursing through this setter
245+
attr(self, "dims") <- NULL
243246
return(self)
244247
}
245248

tests/testthat/test-classes.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,11 @@ test_that("new_setter errors for invalid coerce argument", {
109109
"coerce must be TRUE, FALSE, NULL"
110110
)
111111
})
112+
113+
test_that("Variable@dims can be reset to scalar with NULL", {
114+
v <- quickr:::Variable("double", list(2L, 3L))
115+
expect_identical(v@dims, list(2L, 3L))
116+
v@dims <- NULL
117+
expect_null(v@dims)
118+
expect_true(v@is_scalar)
119+
})

0 commit comments

Comments
 (0)