Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is_bool() #695

Closed
lionel- opened this issue Dec 12, 2018 · 7 comments

Comments

Projects
None yet
3 participants
@lionel-
Copy link
Member

commented Dec 12, 2018

Like is_string(), for checking inputs:

is_bool <- function(x) {
  is_logical(x, n = 1) && !is.na(x)
}

@lionel- lionel- added the vector label Dec 12, 2018

@lionel-

This comment has been minimized.

Copy link
Member Author

commented Dec 12, 2018

And is_number()

is_number <- function(x) {
  is_integerish(x, n = 1, finite = TRUE)
}
@s-fleck

This comment has been minimized.

Copy link

commented Jan 11, 2019

Just my 2c: I would go is_bool() for an arbitrary length 2-value-logic vector and is_scalar_bool() for a vector of length 1 for consistency. I also often define an is_n() function which checks for positive, non-NA integerishs (as in natural numbers). This is something one often actually wants when doing an argument check for scalar integerishs.

@lionel-

This comment has been minimized.

Copy link
Member Author

commented Jan 11, 2019

I prefer is_bool() for single TRUE/FALSE because I think scalar input checks are more important. But we could add an argument to is_logical(), similar to the finite = parameter of numeric predicates.

I like is_n(), maybe it could be is_length()?

@s-fleck

This comment has been minimized.

Copy link

commented Jan 11, 2019

hmm.. from the name I would assume is_length() to be something like as function(x, y) length(x) == y.

I commented on this issue because I am already using a function is_bool()/is_scalar_bool() across my packages, though i have to confess I almost exclusively need the scalar version (as you have pointed out).

Still it would be more consistent with other rlang predicates. When is_integer() checks for an integer vector, why does is_bool() not check for a boolean vector? There is also is.flag() in assertthat (which is basically is_scalar_logical()), but that is also not really optimal in my oppionion :/

@lionel-

This comment has been minimized.

Copy link
Member Author

commented Jan 11, 2019

from the name I would assume is_length() to be something like as function(x, y) length(x) == y.

Good point.

why does is_bool() not check for a boolean vector?

Because those are called logical vectors. I don't think is_bool() would be a consistent name for checking logical vectors. But it is consistent with is_string() / is_character().

@s-fleck

This comment has been minimized.

Copy link

commented Jan 11, 2019

oh sorry I meant is_bool() for true (2 valued/non-na) boolean vectors as opposed to is_logical() which may contain NAs. though I'm not sure how often you really need those in practice (maybe for indexing/subsetting ) ?

@DavisVaughan

This comment has been minimized.

Copy link

commented Jan 24, 2019

+1 for is_bool(), as this would definitely come in handy when type checking some argument inputs!

@lionel- lionel- added this to the 0.4.0 milestone Jun 11, 2019

lionel- added a commit to lionel-/rlang that referenced this issue Jun 11, 2019

lionel- added a commit to lionel-/rlang that referenced this issue Jun 12, 2019

@lionel- lionel- closed this in #786 Jun 12, 2019

lionel- added a commit that referenced this issue Jun 12, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.