是否有与Scala相同的.NET尝试?

时间:2013-12-06 13:30:36

标签: c# .net scala

我一直参加Coursera反应式编程课程,并注意到

之间的相似之处
  1. .NET任务和Scala期货
  2. Observables显然非常相似
  3. .NET IEnumerable和Scala Iterable
  4. 在课程中,Erik Meijer绘制了一个与此类似的表

               Sync     | Async
    Single   | Try      | Future
    Multiple | Iterable | Observable
    

    虽然其余的在.NET中有相同或类似的结构,但我找不到像try这样的东西。有类似的东西吗?

2 个答案:

答案 0 :(得分:4)

内置任何东西,所以你必须自己编写。基于Scala Try我会做类似的事情:

public interface ITry<T> : IEnumerable<T>
{
    T Value { get; }
    ITry<U> SelectMany<U>(Func<T, ITry<U>> bindFunc);
    bool IsSuccess { get; }
}

public class FailedTry<T> : ITry<T>
{
    private readonly Exception ex;
    public FailedTry(Exception ex)
    {
        this.ex = ex;
    }

    public T Value
    {
        get { throw new InvalidOperationException("Can't get value for failed Try"); }
    }

    public ITry<U> SelectMany<U>(Func<T, ITry<U>> bindFunc)
    {
        return new FailedTry<U>(this.ex);
    }

    public bool IsSuccess
    {
        get { return false; }
    }

    public IEnumerator<T> GetEnumerator()
    {
        yield break;
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}

public class SuccessTry<T> : ITry<T>
{
    public SuccessTry(T value)
    {
        this.Value = value;
    }

    public T Value { get; private set; }

    public ITry<U> SelectMany<U>(Func<T, ITry<U>> bindFunc)
    {
        if (bindFunc == null) return new FailedTry<U>(new ArgumentNullException("bindFunc"));
        try
        {
            return bindFunc(this.Value);
        }
        catch (Exception ex)
        {
            return new FailedTry<U>(ex);
        }
    }

    public bool IsSuccess
    {
        get { return true; }
    }

    public IEnumerator<T> GetEnumerator()
    {
        yield return this.Value;
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}

public static class Try
{
    public static ITry<T> Failed<T>(Exception ex)
    {
        return new FailedTry<T>(ex);
    }

    public static ITry<T> Create<T>(Func<T> create)
    {
        if (create == null) return new FailedTry<T>(new ArgumentNullException("create"));
        try
        {
            return new SuccessTry<T>(create());
        }
        catch (Exception ex)
        {
            return new FailedTry<T>(ex);
        }
    }

    public static ITry<U> Select<T, U>(this ITry<T> @try, Func<T, U> mapFunc)
    {
        return @try.SelectMany(v => Create(() => mapFunc(v)));
    }
}

答案 1 :(得分:1)

是这样的吗?

public class Try<TResult>
{
    private readonly bool failure;
    private readonly TResult result;
    private readonly Exception exception;

    protected Try(TResult result)
    {
        this.result = result;
    }

    protected Try(Exception ex)
    {
        this.exception = ex;
        this.failure = true;
    }

    public TResult Result
    {
        get
        {
            if (this.failure) throw new InvalidOperationException();
            return this.result;
        }
    }

    public Exception Exception
    {
        get
        {
            if (!this.failure) throw new InvalidOperationException();
            this.exception;
        }
    }

    public bool Failure
    {
        get
        {
            return this.failure;
        }
    }

    public static Try<TResult> Invoke(Func<TResult> func)
    {
        TResult result;
        try
        {
            result = func();
            return new Try(result);
        }
        catch (Exception ex)
        {
            return new Try(ex);
        }
    }

    public static IEnumerable<Try<TResult>> Invoke(
            IEnumerable<Func<TResult>> funcs)
    {
        return funcs.Select(Invoke);
    }
}

你显然必须重载Invoke才能接受你想要的一系列类型代表。