# Result

{/* Generated from types/std.Rtypes by `just docs-std`. Do not edit this file. */}

A result is `:Ok(value)` or `:Err(error)`, typed `Result<T, E>`. The alias is built in, so every
annotation can use it.

```
@alias Result<T, E> {:Ok(T) | :Err(E)}
```

| Function | Summary |
| --- | --- |
| [`Result.is_ok`](#resultis_ok) | Whether the result is `:Ok`. |
| [`Result.is_err`](#resultis_err) | Whether the result is `:Err`. |
| [`Result.with_default`](#resultwith_default) | The `:Ok` value, or `default` for an `:Err`. |
| [`Result.map`](#resultmap) | Applies `f` to an `:Ok` value and leaves an `:Err` alone. |
| [`Result.map_err`](#resultmap_err) | Applies `f` to an `:Err` value and leaves an `:Ok` alone. |
| [`Result.map_both`](#resultmap_both) | Applies `on_ok` to an `:Ok` value or `on_err` to an `:Err` value. |
| [`Result.map2`](#resultmap2) | Combines two `:Ok` values with `f`, or returns the first `:Err`. |
| [`Result.and_then`](#resultand_then) | Chains a step that can fail: `f` gets the `:Ok` value and returns the next result. |
| [`Result.on_err`](#resulton_err) | Recovers from an `:Err`: `f` gets the error and returns the next result. |

## `Result.is_ok`

### Inline

```r
Result.is_ok <- function<T, E>(result: Result<T, E>) -> logical
```

### Comments

```r
#: <T, E> fn(result: Result<T, E>) -> logical
Result.is_ok <- function(result)
```

Whether the result is `:Ok`.

```r
Result.is_ok(:Ok(1L))
#> TRUE
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L32)

## `Result.is_err`

### Inline

```r
Result.is_err <- function<T, E>(result: Result<T, E>) -> logical
```

### Comments

```r
#: <T, E> fn(result: Result<T, E>) -> logical
Result.is_err <- function(result)
```

Whether the result is `:Err`.

```r
Result.is_err(:Ok(1L))
#> FALSE
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L40)

## `Result.with_default`

### Inline

```r
Result.with_default <- function<T, E>(result: Result<T, E>, default: T) -> T
```

### Comments

```r
#: <T, E> fn(result: Result<T, E>, default: T) -> T
Result.with_default <- function(result, default)
```

The `:Ok` value, or `default` for an `:Err`.

```r
Result.with_default(:Err(:Missing), 0L)
#> 0L
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L48)

## `Result.map`

### Inline

```r
Result.map <- function<A, B, E>(result: Result<A, E>, f: fn(A) -> B) -> Result<B, E>
```

### Comments

```r
#: <A, B, E> fn(result: Result<A, E>, f: fn(A) -> B) -> Result<B, E>
Result.map <- function(result, f)
```

Applies `f` to an `:Ok` value and leaves an `:Err` alone.

```r
Result.map(:Ok(2L), function(x) x * 10L)
#> :Ok(20L)
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L56)

## `Result.map_err`

### Inline

```r
Result.map_err <- function<T, A, B>(result: Result<T, A>, f: fn(A) -> B) -> Result<T, B>
```

### Comments

```r
#: <T, A, B> fn(result: Result<T, A>, f: fn(A) -> B) -> Result<T, B>
Result.map_err <- function(result, f)
```

Applies `f` to an `:Err` value and leaves an `:Ok` alone.

```r
Result.map_err(:Err("disk full"), function(e) :IoError(e))
#> :Err(:IoError("disk full"))
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L64)

## `Result.map_both`

### Inline

```r
Result.map_both <- function<A, B, C, D>(result: Result<A, B>, on_ok: fn(A) -> C, on_err: fn(B) -> D) -> Result<C, D>
```

### Comments

```r
#: <A, B, C, D> fn(result: Result<A, B>, on_ok: fn(A) -> C, on_err: fn(B) -> D) -> Result<C, D>
Result.map_both <- function(result, on_ok, on_err)
```

Applies `on_ok` to an `:Ok` value or `on_err` to an `:Err` value.

```r
Result.map_both(:Err(404L), function(x) x, function(code) Str.concat("HTTP ", Num.to_str(code)))
#> :Err("HTTP 404")
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L72)

## `Result.map2`

### Inline

```r
Result.map2 <- function<A, B, C, E, F>(first: Result<A, E>, second: Result<B, F>, f: fn(A, B) -> C) -> Result<C, E | F>
```

### Comments

```r
#: <A, B, C, E, F> fn(first: Result<A, E>, second: Result<B, F>, f: fn(A, B) -> C) -> Result<C, E | F>
Result.map2 <- function(first, second, f)
```

Combines two `:Ok` values with `f`, or returns the first `:Err`.

```r
Result.map2(Str.to_integer("6"), Str.to_integer("7"), function(a, b) a * b)
#> :Ok(42L)
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L80)

## `Result.and_then`

### Inline

```r
Result.and_then <- function<A, B, E, F>(result: Result<A, E>, f: fn(A) -> Result<B, F>) -> Result<B, E | F>
```

### Comments

```r
#: <A, B, E, F> fn(result: Result<A, E>, f: fn(A) -> Result<B, F>) -> Result<B, E | F>
Result.and_then <- function(result, f)
```

Chains a step that can fail: `f` gets the `:Ok` value and returns the next result. The error type
of the chain joins both, so a step can fail with a reason of its own.

```r
Result.and_then(Str.to_integer("-3"), function(n) if (n > 0L) :Ok(n) else :Err(:NotPositive))
#> :Err(:NotPositive)
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L89)

## `Result.on_err`

### Inline

```r
Result.on_err <- function<T, A, B>(result: Result<T, A>, f: fn(A) -> Result<T, B>) -> Result<T, B>
```

### Comments

```r
#: <T, A, B> fn(result: Result<T, A>, f: fn(A) -> Result<T, B>) -> Result<T, B>
Result.on_err <- function(result, f)
```

Recovers from an `:Err`: `f` gets the error and returns the next result.

```r
Result.on_err(Str.to_integer("x"), function(e) :Ok(0L))
#> :Ok(0L)
```

[Source](https://github.com/felix-andreas/ry/blob/main/types/std.Rtypes#L97)
