Skip to content

Commit

Permalink
ToTry returns Try<Option<A>>
Browse files Browse the repository at this point in the history
Additional ToTry that allows mapping of None
  • Loading branch information
louthy committed Aug 18, 2019
1 parent 433d877 commit 16b9375
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
14 changes: 12 additions & 2 deletions LanguageExt.Core/DataTypes/TryOption/TryOption.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,18 @@ public static EitherUnsafe<L, Option<A>> ToEitherUnsafe<A, L>(this TryOption<A>
}

[Pure]
public static Try<A> ToTry<A>(this TryOption<A> self) => () =>
self.IfFailThrow();
public static Try<Option<A>> ToTry<A>(this TryOption<A> self) => () =>
self.Match(
Some: x => new Result<Option<A>>(Option<A>.Some(x)),
None: () => new Result<Option<A>>(Option<A>.None),
Fail: ex => new Result<Option<A>>(ex));

[Pure]
public static Try<A> ToTry<A>(this TryOption<A> self, Func<A> None) => () =>
self.Match(
Some: x => new Result<A>(x),
None: () => new Result<A>(None()),
Fail: ex => new Result<A>(ex));

[Pure]
public static A IfFailThrow<A>(this TryOption<A> self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1106,8 +1106,18 @@ public static Task<EitherUnsafe<L, Option<A>>> ToEitherUnsafe<A, L>(this TryOpti
Fail: ex => EitherUnsafe<L, Option<A>>.Left(Fail(ex)));

[Pure]
public static TryAsync<A> ToTry<A>(this TryOptionAsync<A> self) =>
async () => (await self.Try()).ToResult();
public static TryAsync<Option<A>> ToTry<A>(this TryOptionAsync<A> self) => () =>
self.Match(
Some: x => new Result<Option<A>>(Option<A>.Some(x)),
None: () => new Result<Option<A>>(Option<A>.None),
Fail: ex => new Result<Option<A>>(ex));

[Pure]
public static TryAsync<A> ToTry<A>(this TryOptionAsync<A> self, Func<A> None) => () =>
self.Match(
Some: x => new Result<A>(x),
None: () => new Result<A>(None()),
Fail: ex => new Result<A>(ex));

[Pure]
public static async Task<A> IfFailThrow<A>(this TryOptionAsync<A> self)
Expand Down
2 changes: 1 addition & 1 deletion LanguageExt.Tests/TryAsyncTEsts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public async void LinqBindingWithTryOptionSuccess()

var res = AsyncHelper.CompletesImmediately(() =>
from a in ma
from b in mb.ToTry()
from b in mb.ToTry(None: () => 0)
select a + b);

var ab = await AsyncHelper.TakesRoughly(2000, () => res.IfFail(0));
Expand Down

0 comments on commit 16b9375

Please sign in to comment.