As a follow-up to my previous question on the usage of &&
and ||
in if
statements, I am wondering whether there is any drawback to replacing &&
and ||
by purely scalar operators in my own package. These should produce exactly the same results as their base counterparts, except that they throw error messages when applied to arguments of length > 1. In particular, they do short-circuiting.
`&&` <- function(x, y) {
if (!x[1]) {
stopifnot(length(x) == 1)
return(FALSE)
}
stopifnot(length(x) == 1)
if (!y[1]) {
stopifnot(length(y) == 1)
return(FALSE)
}
stopifnot(length(y) == 1)
TRUE
}
`||` <- function(x, y) {
if (x[1]) {
stopifnot(length(x) == 1)
return(TRUE)
}
stopifnot(length(x) == 1)
if (y[1]) {
stopifnot(length(y) == 1)
return(TRUE)
}
stopifnot(length(y) == 1)
FALSE
}
Is it safe to do these replacements in a package? Or might that break something? (Except functions that actually rely on the vectorised behavior, obviously...)
Note that I do not plan to export these functions; they are only for internal use in my package.
If I understand it correctly, these replacements should then only affect my own functions. They can't affect anything outside of my package, right?