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
ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1 #48
Comments
I would suggest a warning for symmetry with:
Seems like a similar type of error. Changing the NA behavior seems like a much higher hurdle because if anyone ran into this in the past they would have noticed (e.g. I'm happy to help with this to the extent my abilities and other demands allow me to. |
UPDATE: I just posted 'ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1' to R-devel on 2018-08-28. |
UPDATE 2018-09-12: R-devel r75289 just gained support for this:
|
Hmm... The > Sys.setenv("_R_CHECK_LENGTH_1_LOGIC2_"="true")
> c(TRUE, TRUE) || TRUE
Error in c(TRUE, TRUE) || TRUE :
'length(x) = 2 > 1' in coercion to 'logical(1)'
> TRUE || c(TRUE, TRUE)
[1] TRUE For > c(TRUE, TRUE) && TRUE
Error in c(TRUE, TRUE) && TRUE :
'length(x) = 2 > 1' in coercion to 'logical(1)'
> TRUE && c(TRUE, TRUE)
Error in TRUE && c(TRUE, TRUE) :
'length(x) = 2 > 1' in coercion to 'logical(1)' Verified on R version 3.6.1 (2019-07-05), R version 3.6.1 Patched (2019-09-12 r77183), and R Under development (unstable) (2019-10-12 r77279). UPDATE 2019-10-12: I've filed PR17630 about this problem. UPDATE 2019-10-12: This comment was invalid because:
|
UPDATE 2022-02-23: |
UPDATE 2022-04-27: This will now be an error in R-devel (to become R 4.3.0), cf. Make calling && or || with either argument of length greater than one into an error. |
UPDATE 2022-06-02: This is now an error in R-devel (to become R 4.3.0) and there's no longer an |
Related:
|
Idea
In the spirit of Issue #38 (
if/while (c(TRUE, TRUE)) ...
) of giving a warning (soon error), @hadley proposed in a Tweet:Issue
Today we have that
x || y
performsx[1] || y
forlength(x) > 1
. For instance,This property is symmetric in LHS and RHS (i.e.
y || x
behaves the same) and it also applies tox && y
.The issue is that the above truncation of
x
is completely silent -there's neither an error nor a warning being produced.Discussion/Suggestion
Using
x || y
andx && y
with a non-scalarx
ory
is likely a mistake. Either the code is written assumingx
andy
are scalars, or there is a coding error and vectorized versionsx | y
andx & y
were intended. Shouldx || y
always be considered an mistake iflength(x) != 1
orlength(y) != 1
? If so, should it be a warning or an error? For instance,What about the case where
length(x) == 0
orlength(y) == 0
? Todayx || y
returnsNA
in such cases, e.g.I don't know the background for this behavior, but I'm sure there is an argument behind that one. Maybe it's simply that
||
and&&
should always return a scalar logical and neitherTRUE
norFALSE
can be returned.The text was updated successfully, but these errors were encountered: