Skip to content

Commit 150d522

Browse files
authored
Add %&&% operator (#1822)
Closes #1774
1 parent b7e5b7e commit 150d522

File tree

7 files changed

+50
-1
lines changed

7 files changed

+50
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Suggests:
4646
Enhances:
4747
winch
4848
Encoding: UTF-8
49-
RoxygenNote: 7.3.2
49+
RoxygenNote: 7.3.3
5050
Roxygen: list(markdown = TRUE)
5151
URL: https://rlang.r-lib.org, https://github.com/r-lib/rlang
5252
BugReports: https://github.com/r-lib/rlang/issues

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ S3method(summary,rlang_trace)
9797
S3method(summary,rlang_warning)
9898
export("!!!")
9999
export("!!")
100+
export("%&&%")
100101
export("%<~%")
101102
export("%@%")
102103
export("%@%<-")

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# rlang (development version)
22

3+
* New `%&&%` operator that returns RHS when LHS is non-NULL (#1774, @snystrom).
4+
35
* C code no longer calls `memcpy()` and `memset()` on 0-length R object memory
46
(#1797).
57
* `is_syntactic_literal()` returns `FALSE` for objects with attributes, such as `array(1)` or `factor("x")` (#1817, @jonthegeek).

R/operators.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ if (exists("%||%", envir = baseenv())) {
1919
`%||%` <- get("%||%", envir = baseenv())
2020
}
2121

22+
#' Default value for non-`NULL`
23+
#'
24+
#' This infix operator is the conceptual opposite of `%||%`, providing a fallback
25+
#' only if `x` is defined.
26+
#'
27+
#' @param x,y If `x` is NULL, will return `x`; otherwise returns `y`.
28+
#' @export
29+
#' @name op-null-continuation
30+
#' @seealso [op-null-default]
31+
#' @examples
32+
#' 1 %&&% 2
33+
#' NULL %&&% 2
34+
`%&&%` <- function(x, y) {
35+
if (!is_null(x)) y else x
36+
}
37+
2238
`%|0|%` <- function(x, y) {
2339
if (!length(x)) y else x
2440
}

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,5 +336,6 @@ reference:
336336
- subtitle: Operators
337337
contents:
338338
- "`%||%`"
339+
- "`%&&%`"
339340
- "`%|%`"
340341
- "`%@%`"

man/op-null-continuation.Rd

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-operators.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@ test_that("%@% works with S4 objects (#207)", {
7777
expect_identical(fievel@name, "Bernard")
7878
expect_identical(fievel@species, "MOUSE")
7979
})
80+
81+
test_that("%&&% works", {
82+
expect_null(NULL %&&% 2)
83+
expect_null(1 %&&% NULL)
84+
expect_equal(1 %&&% 2, 2)
85+
})

0 commit comments

Comments
 (0)