Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // (c) zenith-nz 2016
- using System;
- namespace TryReturnType
- {
- /// <summary>
- /// Interface for types that represent a <typeparamref name="T" /> results returned
- /// from operations that can fail
- /// </summary>
- /// <typeparam name="T">Typed result returned by the operation.</typeparam>
- public interface ITryReturn<out T>
- {
- #region Properties, Indexers
- /// <summary>
- /// <see cref="System.Boolean" /> returned by the operation
- /// </summary>
- bool Result { get; }
- /// <summary>
- /// <typeparamref name="T" /> returned by the operation
- /// </summary>
- T TypeResult { get; }
- /// <summary>
- /// Whether the operation returned a <see cref="TypeResult" />
- /// </summary>
- bool HasTypeResult { get; }
- #endregion
- }
- /// <summary>
- /// Interface for types that represent <typeparamref name="T" /> results and
- /// associated <typeparamref name="TDetail" /> returned from operations that can fail
- /// </summary>
- /// <typeparam name="T">Typed result returned by the operation.</typeparam>
- /// <typeparam name="TDetail">Detail returned by the operation.</typeparam>
- public interface ITryReturn<out T, out TDetail> : ITryReturn<T>
- {
- #region Properties, Indexers
- /// <summary>
- /// <typeparamref name="TDetail" /> returned by the operation
- /// </summary>
- TDetail DetailTypeResult { get; }
- /// <summary>
- /// Whether the operation returned a <see cref="DetailTypeResult" />
- /// </summary>
- bool HasDetailTypeResult { get; }
- #endregion
- }
- /// <summary>
- /// Representation of <typeparamref name="T" /> results returned from operations that can fail
- /// </summary>
- /// <typeparam name="T">Typed result returned by the operation that can fail.</typeparam>
- public struct TryReturn<T> : ITryReturn<T>
- {
- /// <summary>
- /// Create a try-return from the <paramref name="typeResult" />.
- /// The <see cref="Result" /> will be set to <c>true</c>
- /// </summary>
- /// <param name="typeResult">Typed result returned from the tried operation.</param>
- public TryReturn(T typeResult)
- {
- Result = true;
- TypeResult = typeResult;
- }
- /// <summary>
- /// Create a try-return from the <paramref name="result" /> and <paramref name="typeResult" />.
- /// </summary>
- /// <param name="result">Boolean result returned from the tried operation (whether try succeeded).</param>
- /// <param name="typeResult">Typed result returned from the tried operation.</param>
- public TryReturn(bool result, T typeResult)
- {
- Result = result;
- TypeResult = typeResult;
- }
- /// <summary>
- /// <see cref="System.Boolean" /> returned by the operation
- /// </summary>
- public bool Result { get; }
- /// <summary>
- /// <typeparamref name="T" /> returned by the operation
- /// </summary>
- public T TypeResult { get; }
- /// <summary>
- /// Whether the method returned a <see cref="ITryReturn{T}.TypeResult" />
- /// </summary>
- public bool HasTypeResult => Result && !Equals(TypeResult, default(T));
- /// <summary>
- /// Convert a <see cref="System.Tuple{Boolean, T}" /> to
- /// the equivalent <see cref="TryReturn{T}" />
- /// </summary>
- /// <param name="t"><see cref="System.Tuple{Boolean, T}" /> to convert.</param>
- /// <returns><paramref name="t" /> as the equivalent <see cref="TryReturn{T}" />.</returns>
- public static implicit operator TryReturn<T>(Tuple<bool, T> t)
- {
- return new TryReturn<T>(t.Item1, t.Item2);
- }
- /// <summary>
- /// Convert a <see cref="TryReturn{T}" /> to
- /// the equivalent <see cref="System.Tuple{Boolean, T}" />
- /// </summary>
- /// <param name="twr"><see cref="TryReturn{T}" /> to convert.</param>
- /// <returns><paramref name="twr" /> as the equivalent <see cref="System.Tuple{Boolean, T}" />.</returns>
- public static implicit operator Tuple<bool, T>(TryReturn<T> twr)
- {
- return new Tuple<bool, T>(twr.Result, twr.TypeResult);
- }
- /// <summary>
- /// Convert a <see cref="TryReturn{T}" /> to the equivalent <see cref="System.Boolean" />,
- /// corresponding to the <see cref="ITryReturn{T}.Result" /> value
- /// </summary>
- /// <param name="twr"><see cref="TryReturn{T}" /> to convert.</param>
- /// <returns><paramref name="twr" /> as the equivalent <see cref="System.Boolean" />.</returns>
- public static implicit operator bool(TryReturn<T> twr)
- {
- return twr.Result;
- }
- }
- /// <summary>
- /// Representation of <typeparamref name="T" /> results and
- /// associated <typeparamref name="TDetail" /> returned from operations that can fail
- /// </summary>
- /// <typeparam name="T">Typed result returned by the operation that can fail.</typeparam>
- /// <typeparam name="TDetail">Detail returned by the operation.</typeparam>
- public struct TryReturn<T, TDetail> : ITryReturn<T, TDetail>
- {
- /// <summary>
- /// Create a try-return from the <paramref name="typeResult" /> and <paramref name="detailResult" />.
- /// The <see cref="Result" /> will be set to <c>true</c>
- /// </summary>
- /// <param name="typeResult">Typed result returned from the tried operation.</param>
- /// <param name="detailResult">Detail returned from the tried operation.</param>
- public TryReturn(T typeResult, TDetail detailResult)
- {
- Result = true;
- TypeResult = typeResult;
- DetailTypeResult = detailResult;
- }
- /// <summary>
- /// Create a try-return from the <paramref name="result" /> and <paramref name="typeResult" /> and
- /// <paramref name="detailResult" />
- /// </summary>
- /// <param name="result">Boolean result returned from the tried operation (whether try succeeded).</param>
- /// <param name="typeResult">Typed result returned from the tried operation.</param>
- /// <param name="detailResult">Detail returned from the tried operation.</param>
- public TryReturn(bool result, T typeResult, TDetail detailResult)
- {
- Result = result;
- TypeResult = typeResult;
- DetailTypeResult = detailResult;
- }
- /// <summary>
- /// <see cref="System.Boolean" /> returned by the operation
- /// </summary>
- public bool Result { get; }
- /// <summary>
- /// <typeparamref name="T" /> returned by the operation
- /// </summary>
- public T TypeResult { get; }
- /// <summary>
- /// Whether the method returned a <see cref="ITryReturn{T}.TypeResult" />
- /// </summary>
- public bool HasTypeResult => Result && !Equals(TypeResult, default(T));
- /// <summary>
- /// <typeparamref name="TDetail" /> returned by the method
- /// </summary>
- public TDetail DetailTypeResult { get; }
- /// <summary>
- /// Whether the method returned a <see cref="ITryReturn{T, TDetail}.DetailTypeResult" />
- /// </summary>
- public bool HasDetailTypeResult => Result && !Equals(TypeResult, default(T));
- /// <summary>
- /// Convert a <see cref="System.Tuple{Boolean, T, TDetail}" /> to
- /// the equivalent <see cref="TryReturn{T, TDetail}" />
- /// </summary>
- /// <param name="t"><see cref="System.Tuple{Boolean, T, TDetail}" /> to convert.</param>
- /// <returns><paramref name="t" /> as the equivalent <see cref="TryReturn{T, TDetail}" />.</returns>
- public static implicit operator TryReturn<T, TDetail>(Tuple<bool, T, TDetail> t)
- {
- return new TryReturn<T, TDetail>(t.Item1, t.Item2, t.Item3);
- }
- /// <summary>
- /// Convert a <see cref="TryReturn{T, TDetail}" /> to
- /// the equivalent <see cref="System.Tuple{Boolean, T, TDetail}" />
- /// </summary>
- /// <param name="twr"><see cref="TryReturn{T, TDetail}" /> to convert.</param>
- /// <returns><paramref name="twr" /> as the equivalent <see cref="System.Tuple{Boolean, T, TDetail}" />.</returns>
- public static implicit operator Tuple<bool, T, TDetail>(TryReturn<T, TDetail> twr)
- {
- return new Tuple<bool, T, TDetail>(twr.Result, twr.TypeResult, twr.DetailTypeResult);
- }
- /// <summary>
- /// Convert a <see cref="TryReturn{T, TDetail}" /> to the equivalent <see cref="System.Boolean" />,
- /// corresponding to the <see cref="ITryReturn{T}.Result" /> value
- /// </summary>
- /// <param name="twr"><see cref="TryReturn{T, TDetail}" /> to convert.</param>
- /// <returns><paramref name="twr" /> as the equivalent <see cref="System.Boolean" />.</returns>
- public static implicit operator bool(TryReturn<T, TDetail> twr)
- {
- return twr.Result;
- }
- }
- /// <summary>
- /// Factory for representations of results of methods that can fail
- /// </summary>
- public static class TryReturn
- {
- #region Static methods
- /// <summary>
- /// Create a try-return with default values.
- /// </summary>
- /// <typeparam name="T">Typed result returned by the operation that can fail.</typeparam>
- /// <returns>Try return with result and typed result values set to their defaults.</returns>
- public static TryReturn<T> Create<T>()
- {
- return new TryReturn<T>(); // Default values used
- //return new TryReturn<T>(false, default(T));
- }
- /// <summary>
- /// Create a try-return from the <paramref name="typeResult" />.
- /// </summary>
- /// <typeparam name="T">Typed result returned by the operation that can fail.</typeparam>
- /// <param name="typeResult">Type result returned from the try.</param>
- /// <returns>Try return with typed result value assigned.</returns>
- public static TryReturn<T> Create<T>(T typeResult)
- {
- return new TryReturn<T>(typeResult);
- }
- /// <summary>
- /// Create a try-return from the <paramref name="result" /> and <paramref name="typeResult" />.
- /// </summary>
- /// <typeparam name="T">Typed result returned by the operation that can fail.</typeparam>
- /// <param name="result">Boolean result returned from the try (whether try succeeded).</param>
- /// <param name="typeResult">Type result returned from the try.</param>
- /// <returns>Try return with result and typed result values assigned.</returns>
- public static TryReturn<T> Create<T>(bool result, T typeResult)
- {
- return new TryReturn<T>(result, typeResult);
- }
- // 2-type:
- /// <summary>
- /// Create a try-return with default values.
- /// </summary>
- /// <typeparam name="T">Typed result returned by the operation that can fail.</typeparam>
- /// <typeparam name="TDetail">Detail returned by the operation.</typeparam>
- /// <returns>Try return with result, typed result, and detail values set to their defaults.</returns>
- public static TryReturn<T, TDetail> Create<T, TDetail>()
- {
- return new TryReturn<T, TDetail>(); // Default values used
- //return new TryReturn<T, TDetail>(false, default(T), default(TDetail));
- }
- /// <summary>
- /// Create a try-return from the <paramref name="typeResult" />.
- /// </summary>
- /// <typeparam name="T">Typed result returned by the operation that can fail.</typeparam>
- /// <typeparam name="TDetail">Detail returned by the operation.</typeparam>
- /// <param name="typeResult">Typed result returned from the tried operation.</param>
- /// <param name="detailResult">Detail returned from the tried operation.</param>
- /// <returns>Try return with typed result and detail values assigned.</returns>
- public static TryReturn<T, TDetail> Create<T, TDetail>(T typeResult, TDetail detailResult)
- {
- return new TryReturn<T, TDetail>(typeResult, detailResult);
- }
- /// <summary>
- /// Create a try-return from the <paramref name="result" /> and <paramref name="typeResult" />.
- /// </summary>
- /// <typeparam name="T">Typed result returned by the operation that can fail.</typeparam>
- /// <typeparam name="TDetail">Detail returned by the operation.</typeparam>
- /// <param name="result">Boolean result returned from the tried operation (whether try succeeded).</param>
- /// <param name="typeResult">Typed result returned from the tried operation.</param>
- /// <param name="detailResult">Detail returned from the tried operation.</param>
- /// <returns>Try return with typed result and detail values assigned.</returns>
- public static TryReturn<T, TDetail> Create<T, TDetail>(bool result, T typeResult, TDetail detailResult)
- {
- return new TryReturn<T, TDetail>(result, typeResult, detailResult);
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement