Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class Code
- {
- // Не метод Expected<T>, что б не писать generic тип для него (типа).
- [Pure]
- public static Expected<T> Expect<T>(Func<T> produce)
- {
- try
- {
- return new Expected<T>(produce());
- }
- catch (Exception e)
- {
- return new Expected<T>(e);
- }
- }
- }
- [PublicAPI]
- public struct Expected<T>
- {
- private readonly T _value;
- private readonly Exception _failure;
- public Expected(T value)
- {
- _value = value;
- _failure = null;
- }
- public Expected([NotNull] Exception failure)
- {
- ThrowIf.Argument.IsNull(failure, nameof(failure));
- _failure = failure;
- _value = default(T);
- }
- // Синоним для Value на случай если по месту вызова более выразительно выглядит Get().
- [Pure]
- public T Get()
- {
- if (null != _failure)
- {
- throw new AggregateException(_failure);
- }
- return _value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement