Skip to content

Commit

Permalink
Making ifSecond parameter as optional on Task Option.MatchAsync (#164)
Browse files Browse the repository at this point in the history
Let's make the second parameter optional to avoid forcing developers to
specify the second function that does nothing

e.g.:
```
await myOption.MatchAsync(async r =>
    {
        await something...
        //do something else
    },
    _ => Task.CompletedTask
);
```
=>
```
await myOption.MatchAsync(async r =>
{
    await something...
    //do something else
});
```

Benchmark results:
.NET 6

![image](https://github.com/MewsSystems/FuncSharp/assets/40407201/598afb82-1f68-41ef-b5c1-e566634a65fc)
.NET 8

![image](https://github.com/MewsSystems/FuncSharp/assets/40407201/c588ac76-9395-4c10-a4d5-71d11e6c569b)
  • Loading branch information
vaclavpacholik authored Feb 7, 2024
1 parent b7fa1ba commit 1abf800
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/FuncSharp/Option/IOptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,15 +369,18 @@ public static async Task<Option<B>> MapAsync<A, B>(this Option<A> option, Func<A
}

[Pure]
public static async Task MatchAsync<A>(this Option<A> option, Func<A, Task> ifFirst, Func<Unit, Task> ifSecond)
public static async Task MatchAsync<A>(this Option<A> option, Func<A, Task> ifFirst, Func<Unit, Task> ifSecond = null)
{
if (option.NonEmpty)
{
await ifFirst(option.Value);
}
else
{
await ifSecond(Unit.Value);
if (ifSecond != null)
{
await ifSecond(Unit.Value);
}
}
}

Expand Down

0 comments on commit 1abf800

Please sign in to comment.