Skip to content

Conversation

DavisVaughan
Copy link
Member

@DavisVaughan DavisVaughan commented Sep 30, 2025

In vctrs, we are trying to be fairly strict about the type of condition in vec_if_else(). In particular, we disallow arrays from being passed as condition, even 1D arrays (we do this consistently across vec_if_else(), list_combine(), vec_case_when(), and vec_replace_when()`, all of which can take logical "condition" indices).

https://github.com/r-lib/vctrs/blob/f27ab24c7eb0b1d78d48bd1d39175a1ecf7d4411/src/slice-assign.c#L964-L971

Unfortunately, obj_type_friendly() currently reports 1D arrays as identical to vectors resulting in the following not-useful error message

     Error in `if_else()`:
     ! `condition` must be a logical vector, not a logical vector.

I'd argue that 1D arrays should be reported as arrays by obj_type_friendly(). In other words, we'd have:

  • n_dim = 0 gives vector
  • n_dim = 1 gives array
  • n_dim = 2 gives matrix
  • n_dim = 3+ gives array

This feels right to me and allows us to report a useful distinction between vectors and 1D arrays.

Copy link
Member

@lionel- lionel- left a comment

Choose a reason for hiding this comment

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

Since array vectors print the same way as vectors, it seems useful to distinguish them from arrays (which print differently) in a more explicit way. Some suggestions inside.

if (n_dim == 0) {
return(add_length("a list"))
} else if (n_dim == 1) {
return("a list array")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return("a list array")
return("an array vector of type list")

Copy link
Member Author

Choose a reason for hiding this comment

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

  • n_dim = 0 gives vector
  • n_dim = 2 gives matrix
  • n_dim = N gives {N}D array

Here's what we agreed on

I like this because it also lets you differentiate between arrays of different dimensionality which we couldn't do before. Like "i expect a 3D array, not a 4D array"

@DavisVaughan DavisVaughan changed the title Report 1D arrays differently from vectors in obj_type_friendly() Report array dimensionality in obj_type_friendly() Oct 2, 2025
@DavisVaughan DavisVaughan merged commit a9b61fd into main Oct 2, 2025
11 checks passed
@DavisVaughan DavisVaughan deleted the feature/one-d-array-friendly-type branch October 2, 2025 15:58
DavisVaughan added a commit to r-lib/vctrs that referenced this pull request Oct 2, 2025
DavisVaughan added a commit to r-lib/vctrs that referenced this pull request Oct 2, 2025
* Use up to date standalone files

* Inherit improved rlang error messages for arrays

From r-lib/rlang#1832
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.

2 participants