---
title: Std.Result
---

Computations that can succeed (Ok) or fail (Err) with a typed error.

## Types

### Result

```saga
type Result a b =
  | Ok a
  | Err b
```

A value that is either a success (Ok) or a failure (Err).

## Functions

### is_ok

```saga
fun is_ok : Result a b -> Bool
```

Returns True if the value is Ok.

### is_err

```saga
fun is_err : Result a b -> Bool
```

Returns True if the value is Err.

### map

```saga
fun map : (f: a -> c) -> (r: Result a b) -> Result c b
```

Applies a function to the Ok value, leaving Err unchanged.

### map_err

```saga
fun map_err : (f: b -> c) -> (r: Result a b) -> Result a c
```

Applies a function to the Err value, leaving Ok unchanged.

### unwrap

```saga
fun unwrap : (r: Result a b) -> a where {b: Debug}
```

Returns the Ok value. Panics on Err.

### unwrap_or

```saga
fun unwrap_or : (default: a) -> (r: Result a b) -> a
```

Returns the Ok value or a default if Err.

### and_then

```saga
fun and_then : (f: a -> Result c b) -> (r: Result a b) -> Result c b
```

Chains a function that returns Result. Short-circuits on Err.

### or_else

```saga
fun or_else : (fallback: Result a b) -> (r: Result a b) -> Result a b
```

Returns the first Ok value, or tries the fallback on Err.

### to_maybe

```saga
fun to_maybe : Result a b -> Maybe a
```

Converts a Result to a Maybe, discarding the error.

### from_maybe

```saga
fun from_maybe : (err: e) -> (m: Maybe a) -> Result a e
```

Converts a Maybe to a Result, using the given error if Nothing.

### flatten

```saga
fun flatten : Result (Result a b) b -> Result a b
```

Flattens a nested Result.

