Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 10 additions & 1 deletion R/r2f-constructors.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,16 @@ r2f_handlers[["array"]] <- function(args, scope = NULL, ...) {

out <- r2f(args$data, scope, ...)
if (!passes_as_scalar(out@value)) {
stop("array(data=) must be a scalar for now")
# R semantics: `array()` flattens its input (dropping dim) then reshapes.
# We implement this as `reshape()`; recycling is not supported here.
dim_vec <- r2f(args$dim, scope, ...)
if (is.null(dim_vec@value) || dim_vec@value@mode != "integer") {
stop("array(dim=) must be an integer vector", call. = FALSE)
}
if (dim_vec@value@rank != 1L) {
stop("array(dim=) must be a 1-d integer vector", call. = FALSE)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Allow numeric/scalar dim values for array() reshape

The new validation rejects any dim that isn't an integer vector with rank 1, but base R allows numeric (double) dims and also allows a scalar dim (length‑1) which is coerced to an integer vector. This means common calls like array(as.double(x), dim = c(2, 3)) or array(as.double(x), dim = 2) will now error at compile time even though int() would coerce successfully. Consider relaxing the check to accept numeric/scalar dims and coercing to integer rather than stopping.

Useful? React with 👍 / 👎.

}
out <- Fortran(glue("reshape({out}, int({dim_vec}))"), out@value)
}

out@value <- Variable(
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-array-reshape.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
test_that("array() supports reshaping non-scalar data", {
fn <- function(x) {
declare(type(x = integer(2L, 3L, 4L)))
array(as.double(x), dim = c(2L, 3L, 4L))
}

set.seed(1)
x <- array(sample(1:10, 24, replace = TRUE), dim = c(2L, 3L, 4L))
expect_quick_identical(fn, list(x))
})
1 change: 0 additions & 1 deletion tests/testthat/test-r2f-r-attr.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ test_that("r2f() attaches `r` metadata for bind(c) logical symbols", {
expect_true(inherits(a_var, Variable))
expect_identical(a_var@r, quote(m))
})