bang: Extract Result or Return

Description Usage Arguments Details Value Functions Examples

Description

Returns the value contained inside of an Result or Option Enum or returns if failure.

Usage

1
2
3
4
5
## S3 method for class 'Result'
!x, ...

## S3 method for class 'Option'
!x, ...

Arguments

x

Enumerated value of type Result or Option to unwrap

...

objects to be passed to methods.

Details

This is similar to unwrap for Result and Option objects. However, an Err or None variant does not cause execution to stop. Instead, the parent function immediately returns the Enum intact. Inspired by the ? operator in Rust.

Value

an object of any class or x if failure.

Functions

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
is_big <- function(x) {
  if (x > 10) return(Ok(x))
  Err("This is small!")
}

# If 'x' is greater than 10, the value will be printed.
# Otherwise, an error is returned.
print_big <- function(x) {
  print(!is_big(x))
}

matchr documentation built on Sept. 9, 2021, 5:07 p.m.

Related to bang in matchr...