Created
December 16, 2013 09:31
-
-
Save shishkin/7984435 to your computer and use it in GitHub Desktop.
Functional error handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using FSharpx; | |
using Microsoft.FSharp.Core; | |
using NUnit.Framework; | |
namespace FunctionalErrorHandling | |
{ | |
public static class Extensions | |
{ | |
public static Func<Unit, T> AddArg<T>(this Func<T> f) | |
{ | |
return _ => f(); | |
} | |
public static FSharpChoice<T, Exception> Try<T>(this Func<T> f) | |
{ | |
return f.AddArg().Try()(null); | |
} | |
} | |
public class Try | |
{ | |
public static FSharpChoice<T, Exception> Func<T>(Func<T> f) | |
{ | |
return f.Try(); | |
} | |
} | |
public class TryTests | |
{ | |
private static readonly int Zero = 0; | |
[Test] | |
public void Try_does_not_throw() | |
{ | |
Try.Func(() => 3/Zero); | |
} | |
[Test] | |
public void Select_does_not_throw() | |
{ | |
Try.Func(() => 3).Select(x => x/Zero); | |
} | |
} | |
} |
/cc @mausch
what's the error?
ok I can reproduce.
After looking at this I'm not really sure if it is by design.
Maybe @mausch knows more.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FSharpChoice isn't suitable for exception handling as is, because FSharpChoice.Select throws when Try.Select shouldn't.