---
title: "aiken/option"
---

> Documentation Index
> Fetch the complete documentation index at: https://aiken-lang.com/llms.txt
> Use this file to discover all available pages before exploring further.

# aiken/option

A type to capture optional results; useful for handling errors.

Note that the `Option` type and its constructors are readily available in Aiken. They are part of the [Prelude](https://aiken-lang.com/prelude/#Option) module imported by default in every module.

## Functions

### Inspecting

### `is_none` {#is_none}

```aiken
is_none(self: Option<a>) -> Bool
```

Asserts whether an option is `None`.

### `is_some` {#is_some}

```aiken
is_some(self: Option<a>) -> Bool
```

Asserts whether an option is `Some`, irrespective of the value it contains.

### Combining

### `and_then` {#and_then}

```aiken
and_then(self: Option<a>, then: fn(a) -> Option<result>) -> Option<result>
```

Chain together many computations that may fail.

```aiken
self
  |> dict.get(policy_id)
  |> option.and_then(dict.get(_, asset_name))
  |> option.or_else(0)
```

### `choice` {#choice}

```aiken
choice(self: List<Option<a>>) -> Option<a>
```

Picks the first element which is not None. If there's no such element, return None.

```aiken
option.choice([]) == None
option.choice([Some(14), Some(42)]) == Some(14)
option.choice([None, Some(42)]) == Some(42)
option.choice([None, None]) == None
```

### `flatten` {#flatten}

```aiken
flatten(opt: Option<Option<a>>) -> Option<a>
```

Converts from `Option<Option<a>>` to `Option<a>`.

```aiken
option.flatten(Some(Some(42))) == Some(42)
option.flatten(Some(None)) == None
option.flatten(None) == None
```

Flattening only removes one level of nesting at a time:

```aiken
flatten(Some(Some(Some(42)))) == Some(Some(42))
Some(Some(Some(42))) |> flatten |> flatten == Some(42)
```

### `map` {#map}

```aiken
map(self: Option<a>, with: fn(a) -> result) -> Option<result>
```

Apply a function to the inner value of an [`Option`](#option)

```aiken
option.map(None, fn(n) { n * 2 }) == None
option.map(Some(14), fn(n) { n * 2 }) == Some(28)
```

### `map2` {#map2}

```aiken
map2(
  opt_a: Option<a>,
  opt_b: Option<b>,
  with: fn(a, b) -> result,
) -> Option<result>
```

Combine two [`Option`](#option) together.

```aiken
type Foo {
  Foo(Int, Int)
}

option.map2(Some(14), Some(42), Foo) == Some(Foo(14, 42))
option.map2(None, Some(42), Foo) == None
option.map2(Some(14), None, Foo) == None
```

### `map3` {#map3}

```aiken
map3(
  opt_a: Option<a>,
  opt_b: Option<b>,
  opt_c: Option<c>,
  with: fn(a, b, c) -> result,
) -> Option<result>
```

Combine three [`Option`](#option) together.

```aiken
type Foo {
  Foo(Int, Int, Int)
}

option.map3(Some(14), Some(42), Some(1337), Foo) == Some(Foo(14, 42, 1337))
option.map3(None, Some(42), Some(1337), Foo) == None
option.map3(Some(14), None, None, Foo) == None
```

### `or_try` {#or_try}

```aiken
or_try(self: Option<a>, compute_default: fn() -> Option<a>) -> Option<a>
```

Like [`or_else`](#or_else) but allows returning an `Option`.
This is effectively mapping the error branch.

```aiken
option.or_try(None, fn(_) { Some("aiken") }) == Some("aiken")
option.or_try(Some(42), fn(_) { Some(14) }) == Some(42)
option.or_try(None, fn (_) { fail }) → 💥
```

### Transforming

### `or_else` {#or_else}

```aiken
or_else(self: Option<a>, default: a) -> a
```

Provide a default value, turning an optional value into a normal value.

```aiken
option.or_else(None, "aiken") == "aiken"
option.or_else(Some(42), 14) == 42
```

Source: https://aiken-lang.com/stdlib/aiken/option/index.mdx
