greta-dev / greta

simple and scalable statistical modelling in R
https://greta-stats.org
Other
527 stars 63 forks source link

Use `%||%` to replace `if (is.null(x)) {do else}` pattern #630

Closed njtierney closed 2 months ago

njtierney commented 4 months ago

E.g.,

if (is.null(dim)) {
        dim <- c(1, 1)
      }

# ...

# store array (updates dim)
      if (is.null(value)) {
        value <- unknowns(dim = dim)
      }

would be

dim <- dim %||% c(1,1)

# store array (updates dim)
value <- value %||% unknowns(dim = dim)

%||% is available in R 4.4.0, or can be re-exported from rlang or implemented locally easily enough.