> For the complete documentation index, see [llms.txt](https://gabriel-schade-cardoso.gitbook.io/tango/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gabriel-schade-cardoso.gitbook.io/tango/modules/option/bind.md).

# Bind

Creates a new [`Option<T>`](https://github.com/gabrielschade/tango/tree/379cc4a38ae47796971eb875ec66e7dc053a9081/Types/Option/Introduction.html) whose value is the result of applying the given `binder` function to `Option<T>.Some` value when `option IsSome`.

Otherwise returns an `Option<T>.None()`.

| Parameters                                                       | Returns          |
| ---------------------------------------------------------------- | ---------------- |
| <p>Func\<T, Option\<TResult>> binder</p><p>Option\<T> option</p> | Option\<TResult> |

## Como usar

This function is usually used to modify an option value by using an function that receive a regular value and returns an optional.

It works like a `Map` function, but in this case the function returns an option value.

&#x20;**When the option value IsSome and binder returns IsSome**&#x20;

```csharp
Option<int> SquareWhenEven(int value)
{
    if(element % 2 == 0)
        return value * value;
    else
        return Option<int>.None();
}

Option<int> optionValue = 4;
Option<int> result = optionValue.Bind(SquareWhenEven);

//result.IsSome = true
//result.Some = 8
```

&#x20;**When the option value IsSome and binder returns IsNone**

```csharp
Option<int> SquareWhenEven(int value)
{
    if(element % 2 == 0)
        return value * value;
    else
        return Option<int>.None();
}

Option<int> optionValue = 5;
Option<int> result = optionValue.Bind(SquareWhenEven);

//result.IsSome = false
//result.IsNone = true
```

&#x20;**When the option value IsNone**&#x20;

```csharp
Option<int> SquareWhenEven(int value)
{
    if(element % 2 == 0)
        return value * value;
    else
        return Option<int>.None();
}

Option<int> optionValue = Option<int>.None();
Option<int> result = optionValue.Bind(SquareWhenEven);

//result.IsSome = false
//result.IsNone = true
```
