c# - 是否有与 Scala 的 Try 等效的 .NET?

标签 c# .net scala

我一直在参加 Coursera 响应式(Reactive)编程类(class),并注意到两者之间的相似之处

  1. .NET 任务和 Scala Futures
  2. Observables 显然非常相似
  3. .NET IEnumerable 和 Scala Iterable

Erik Meijer 在类(class)中画了一张类似这样的表格

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

虽然其余的在 .NET 中具有等效或相似的构造,但我找不到类似 try 的东西。是否存在类似的东西?

最佳答案

没有内置的东西,所以你必须自己写。基于 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)));
    }
}

关于c# - 是否有与 Scala 的 Try 等效的 .NET?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20425170/

相关文章:

javascript - Blazor 中的级联下拉菜单

c# - "InvalidOperationException: IDX20803: Unable to obtain configuration from: ' [PII 被隐藏] '"

c# - Windows 窗体 : ShowDialog - dialog doesn't show up

.NET Framework 和版本兼容性

scala - 尝试连接到 MySql 时出现光滑异常

c# - JSON 数据结构 - JSON 到对象 - 最佳实践

.net - 在 VS2010 数据库项目中部署时设置 DefaultDataPath 和 DefaultLogPath

c# - 什么是NullReferenceException,如何解决?

scala - 使用 scala fs2 文件流从文件中删除过滤行

scala - 如何使用 SBT 对 fat jar 运行 ScalaTest 测试?