null_or: Null-safe (coalescing) operator

null_orR Documentation

Null-safe (coalescing) operator

Description

Returns the left-hand side (a) if it is not NULL, otherwise returns the right-hand side (b). This operator is useful for setting default values in a concise manner.

Usage

a %||% b

Arguments

a

The primary object or value.

b

The default object or value to return if a is NULL.

Details

Null-coalescing operator

This implementation mirrors the behavior of ⁠%||%⁠ used in several R ecosystems (e.g., rlang, purrr) but is defined here for convenience within the TemporalForest package.

Value

a if not NULL, otherwise b.

Conflicts

The ⁠%||%⁠ operator name is also used in packages such as rlang and purrr. If you load those packages after TemporalForest, they may mask this operator. To avoid ambiguity, call it explicitly using ⁠TemporalForest::\⁠%||%\“.

Examples

a <- NULL
b <- 5
a %||% b   # Returns 5

x <- 10
y <- 20
x %||% y   # Returns 10

# Safe usage when multiple packages define %||%
TemporalForest::`%||%`(NULL, "default")

TemporalForest documentation built on Dec. 23, 2025, 1:06 a.m.