[英]Is there a .NET equivalent of Scala's Try?
我参加了Coursera反应式编程课程,并注意到了两者之间的相似之处
在课程中,Erik Meijer画了一张与此类似的桌子
Sync | Async
Single | Try | Future
Multiple | Iterable | Observable
尽管其余的在.NET中具有相同或相似的构造,但我找不到任何尝试。 是否存在类似的东西?
没有内置任何内容,因此您必须自己编写。 基于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)));
}
}
是这样吗
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
才能接受所需的类型化委托的范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.