rlang icon indicating copy to clipboard operation
rlang copied to clipboard

add `check_length()` to `standalone-types-check.R`

Open ateucher opened this issue 2 years ago • 3 comments

It would be nice to be able to check if a vector is of a required length (exact, min or max). The scalar checkers check_name(), check_string() etc., ensure an object is of length 1, but being able to check length constraints > 1 would be helpful. I could see this being implemented as something like:

check_length(x, ..., length, arg = caller_arg(x), call = caller_env())

where length could be either a scalar integer value to specify an exact required length, or a length 2 vector supplying an allowed range of lengths (which could include -Inf or Inf if only a minimum or maximum length is required).

Alternatively, a length argument could be added to the vector checkers such as check_character() and check_logical().

ateucher avatar Apr 18 '23 23:04 ateucher

I think that makes sense. I'd just put length on the left of ... to avoid the repetition in calls, e.g. check_length(x, length = 3) vs check_length(x, 3). I'm not sure we need to allow for ranges of length though. But sometimes we want to allow multiple lengths when recycling is allowed, e.g. length 1 or length n. So maybe it should rather be length = c(1, n)?

lionel- avatar Apr 28 '23 09:04 lionel-

I hadn't thought about the recycling situation, but it's a good point! I proposed the range to address situations like this, where there is a minimum length required (in this case a character vector with length > 0). I agree that requiring a specific range, or even a maximum length is probably not a common scenario, but requiring a non-empty vector that could be any length >=1 is fairly common. Maybe for that case an allow_empty argument could be added to check_character() etc?

ateucher avatar Apr 28 '23 16:04 ateucher

I think check_character() should have a way to disallow character(0), i.e. length 1 or more.

the use case is that I want to accept NULL (length 0), and character of length 1 or more (not character(0))

I think that allow_empty is confusing since check_string(allow_empty = FALSE) is used to disallow ""

olivroy avatar May 23 '24 19:05 olivroy