[Rd] ROBUSTNESS: x || y and x && y to give warning/error if length(x) != 1 or length(y) != 1
jori@mey@ @ending from gm@il@com
Thu Aug 30 13:56:48 CEST 2018
I have to agree with Emil here. && and || are short circuited like in C and
C++. That means that
TRUE || c(TRUE, FALSE)
FALSE && c(TRUE, FALSE)
cannot give an error because the second part is never evaluated. Throwing a
warning or error for
c(TRUE, FALSE) || TRUE
would mean that the operator gives a different result depending on the
order of the objects, breaking the symmetry. Also that would be undesirable.
Regarding logical(0): per the documentation, it is indeed so that ||, &&
and isTRUE always return a length-one logical vector. Hence the NA.
On a sidenote: there is no such thing as a scalar in R. What you call
scalar, is really a length-one vector. That seems like a detail, but is
important in understanding why this admittedly confusing behaviour actually
makes sense within the framework of R imho. I do understand your objections
and suggestions, but it would boil down to removing short circuited
operators from R.
My 2 cents.
On Wed, Aug 29, 2018 at 5:03 AM Henrik Bengtsson <henrik.bengtsson using gmail.com>
> # Issue
> 'x || y' performs 'x || y' for length(x) > 1. For instance (here
> using R 3.5.1),
> > c(TRUE, TRUE) || FALSE
>  TRUE
> > c(TRUE, FALSE) || FALSE
>  TRUE
> > c(TRUE, NA) || FALSE
>  TRUE
> > c(FALSE, TRUE) || FALSE
>  FALSE
> This property is symmetric in LHS and RHS (i.e. 'y || x' behaves the
> same) and it also applies to 'x && y'.
> Note also how the above truncation of 'x' is completely silent -
> there's neither an error nor a warning being produced.
> # Discussion/Suggestion
> Using 'x || y' and 'x && y' with a non-scalar 'x' or 'y' is likely a
> mistake. Either the code is written assuming 'x' and 'y' are scalars,
> or there is a coding error and vectorized versions 'x | y' and 'x & y'
> were intended. Should 'x || y' always be considered an mistake if
> 'length(x) != 1' or 'length(y) != 1'? If so, should it be a warning
> or an error? For instance,
> > x <- c(TRUE, TRUE)
> > y <- FALSE
> > x || y
> Error in x || y : applying scalar operator || to non-scalar elements
> Execution halted
> What about the case where 'length(x) == 0' or 'length(y) == 0'? Today
> 'x || y' returns 'NA' in such cases, e.g.
> > logical(0) || c(FALSE, NA)
>  NA
> > logical(0) || logical(0)
>  NA
> > logical(0) && logical(0)
>  NA
> 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 neither TRUE nor FALSE can
> be returned.
> PS. This is in the same vein as
> - in R (>=3.4.0) we now get that if (1:2 == 1) ... is an error if
> R-devel using r-project.org mailing list
Department of Data Analysis and Mathematical Modelling
Coupure Links 653, B-9000 Gent (Belgium)
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
[[alternative HTML version deleted]]
More information about the R-devel