Skip to content

Instantly share code, notes, and snippets.

@shishkin
Created December 16, 2013 09:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shishkin/7984435 to your computer and use it in GitHub Desktop.
Save shishkin/7984435 to your computer and use it in GitHub Desktop.
Functional error handling
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);
}
}
}
@forki
Copy link

forki commented Dec 16, 2013

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