Advertisement
Guest User

TryReturn

a guest
Aug 24th, 2016
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.45 KB | None | 0 0
  1. // (c) zenith-nz 2016
  2.  
  3. using System;
  4.  
  5. namespace TryReturnType
  6. {
  7.     /// <summary>
  8.     /// Interface for types that represent a <typeparamref name="T" /> results returned
  9.     /// from operations that can fail
  10.     /// </summary>
  11.     /// <typeparam name="T">Typed result returned by the operation.</typeparam>
  12.     public interface ITryReturn<out T>
  13.     {
  14.         #region Properties, Indexers
  15.  
  16.         /// <summary>
  17.         /// <see cref="System.Boolean" /> returned by the operation
  18.         /// </summary>
  19.         bool Result { get; }
  20.  
  21.         /// <summary>
  22.         /// <typeparamref name="T" /> returned by the operation
  23.         /// </summary>
  24.         T TypeResult { get; }
  25.  
  26.         /// <summary>
  27.         /// Whether the operation returned a <see cref="TypeResult" />
  28.         /// </summary>
  29.         bool HasTypeResult { get; }
  30.  
  31.         #endregion
  32.     }
  33.  
  34.     /// <summary>
  35.     /// Interface for types that represent <typeparamref name="T" /> results and
  36.     /// associated <typeparamref name="TDetail" /> returned from operations that can fail
  37.     /// </summary>
  38.     /// <typeparam name="T">Typed result returned by the operation.</typeparam>
  39.     /// <typeparam name="TDetail">Detail returned by the operation.</typeparam>
  40.     public interface ITryReturn<out T, out TDetail> : ITryReturn<T>
  41.     {
  42.         #region Properties, Indexers
  43.  
  44.         /// <summary>
  45.         /// <typeparamref name="TDetail" /> returned by the operation
  46.         /// </summary>
  47.         TDetail DetailTypeResult { get; }
  48.  
  49.         /// <summary>
  50.         /// Whether the operation returned a <see cref="DetailTypeResult" />
  51.         /// </summary>
  52.         bool HasDetailTypeResult { get; }
  53.  
  54.         #endregion
  55.     }
  56.  
  57.     /// <summary>
  58.     /// Representation of <typeparamref name="T" /> results returned from operations that can fail
  59.     /// </summary>
  60.     /// <typeparam name="T">Typed result returned by the operation that can fail.</typeparam>
  61.     public struct TryReturn<T> : ITryReturn<T>
  62.     {
  63.         /// <summary>
  64.         /// Create a try-return from the <paramref name="typeResult" />.
  65.         /// The <see cref="Result" /> will be set to <c>true</c>
  66.         /// </summary>
  67.         /// <param name="typeResult">Typed result returned from the tried operation.</param>
  68.         public TryReturn(T typeResult)
  69.         {
  70.             Result = true;
  71.             TypeResult = typeResult;
  72.         }
  73.  
  74.         /// <summary>
  75.         /// Create a try-return from the <paramref name="result" /> and <paramref name="typeResult" />.
  76.         /// </summary>
  77.         /// <param name="result">Boolean result returned from the tried operation (whether try succeeded).</param>
  78.         /// <param name="typeResult">Typed result returned from the tried operation.</param>
  79.         public TryReturn(bool result, T typeResult)
  80.         {
  81.             Result = result;
  82.             TypeResult = typeResult;
  83.         }
  84.  
  85.         /// <summary>
  86.         /// <see cref="System.Boolean" /> returned by the operation
  87.         /// </summary>
  88.         public bool Result { get; }
  89.  
  90.         /// <summary>
  91.         /// <typeparamref name="T" /> returned by the operation
  92.         /// </summary>
  93.         public T TypeResult { get; }
  94.  
  95.         /// <summary>
  96.         /// Whether the method returned a <see cref="ITryReturn{T}.TypeResult" />
  97.         /// </summary>
  98.         public bool HasTypeResult => Result && !Equals(TypeResult, default(T));
  99.  
  100.         /// <summary>
  101.         /// Convert a <see cref="System.Tuple{Boolean, T}" /> to
  102.         /// the equivalent <see cref="TryReturn{T}" />
  103.         /// </summary>
  104.         /// <param name="t"><see cref="System.Tuple{Boolean, T}" /> to convert.</param>
  105.         /// <returns><paramref name="t" /> as the equivalent <see cref="TryReturn{T}" />.</returns>
  106.         public static implicit operator TryReturn<T>(Tuple<bool, T> t)
  107.         {
  108.             return new TryReturn<T>(t.Item1, t.Item2);
  109.         }
  110.  
  111.         /// <summary>
  112.         /// Convert a <see cref="TryReturn{T}" /> to
  113.         /// the equivalent <see cref="System.Tuple{Boolean, T}" />
  114.         /// </summary>
  115.         /// <param name="twr"><see cref="TryReturn{T}" /> to convert.</param>
  116.         /// <returns><paramref name="twr" /> as the equivalent <see cref="System.Tuple{Boolean, T}" />.</returns>
  117.         public static implicit operator Tuple<bool, T>(TryReturn<T> twr)
  118.         {
  119.             return new Tuple<bool, T>(twr.Result, twr.TypeResult);
  120.         }
  121.  
  122.         /// <summary>
  123.         /// Convert a <see cref="TryReturn{T}" /> to the equivalent <see cref="System.Boolean" />,
  124.         /// corresponding to the <see cref="ITryReturn{T}.Result" /> value
  125.         /// </summary>
  126.         /// <param name="twr"><see cref="TryReturn{T}" /> to convert.</param>
  127.         /// <returns><paramref name="twr" /> as the equivalent <see cref="System.Boolean" />.</returns>
  128.         public static implicit operator bool(TryReturn<T> twr)
  129.         {
  130.             return twr.Result;
  131.         }
  132.     }
  133.  
  134.     /// <summary>
  135.     /// Representation of <typeparamref name="T" /> results and
  136.     /// associated <typeparamref name="TDetail" /> returned from operations that can fail
  137.     /// </summary>
  138.     /// <typeparam name="T">Typed result returned by the operation that can fail.</typeparam>
  139.     /// <typeparam name="TDetail">Detail returned by the operation.</typeparam>
  140.     public struct TryReturn<T, TDetail> : ITryReturn<T, TDetail>
  141.     {
  142.         /// <summary>
  143.         /// Create a try-return from the <paramref name="typeResult" /> and <paramref name="detailResult" />.
  144.         /// The <see cref="Result" /> will be set to <c>true</c>
  145.         /// </summary>
  146.         /// <param name="typeResult">Typed result returned from the tried operation.</param>
  147.         /// <param name="detailResult">Detail returned from the tried operation.</param>
  148.         public TryReturn(T typeResult, TDetail detailResult)
  149.         {
  150.             Result = true;
  151.             TypeResult = typeResult;
  152.             DetailTypeResult = detailResult;
  153.         }
  154.  
  155.         /// <summary>
  156.         /// Create a try-return from the <paramref name="result" /> and <paramref name="typeResult" /> and
  157.         /// <paramref name="detailResult" />
  158.         /// </summary>
  159.         /// <param name="result">Boolean result returned from the tried operation (whether try succeeded).</param>
  160.         /// <param name="typeResult">Typed result returned from the tried operation.</param>
  161.         /// <param name="detailResult">Detail returned from the tried operation.</param>
  162.         public TryReturn(bool result, T typeResult, TDetail detailResult)
  163.         {
  164.             Result = result;
  165.             TypeResult = typeResult;
  166.             DetailTypeResult = detailResult;
  167.         }
  168.  
  169.         /// <summary>
  170.         /// <see cref="System.Boolean" /> returned by the operation
  171.         /// </summary>
  172.         public bool Result { get; }
  173.  
  174.         /// <summary>
  175.         /// <typeparamref name="T" /> returned by the operation
  176.         /// </summary>
  177.         public T TypeResult { get; }
  178.  
  179.         /// <summary>
  180.         /// Whether the method returned a <see cref="ITryReturn{T}.TypeResult" />
  181.         /// </summary>
  182.         public bool HasTypeResult => Result && !Equals(TypeResult, default(T));
  183.  
  184.         /// <summary>
  185.         /// <typeparamref name="TDetail" /> returned by the method
  186.         /// </summary>
  187.         public TDetail DetailTypeResult { get; }
  188.  
  189.         /// <summary>
  190.         /// Whether the method returned a <see cref="ITryReturn{T, TDetail}.DetailTypeResult" />
  191.         /// </summary>
  192.         public bool HasDetailTypeResult => Result && !Equals(TypeResult, default(T));
  193.  
  194.         /// <summary>
  195.         /// Convert a <see cref="System.Tuple{Boolean, T, TDetail}" /> to
  196.         /// the equivalent <see cref="TryReturn{T, TDetail}" />
  197.         /// </summary>
  198.         /// <param name="t"><see cref="System.Tuple{Boolean, T, TDetail}" /> to convert.</param>
  199.         /// <returns><paramref name="t" /> as the equivalent <see cref="TryReturn{T, TDetail}" />.</returns>
  200.         public static implicit operator TryReturn<T, TDetail>(Tuple<bool, T, TDetail> t)
  201.         {
  202.             return new TryReturn<T, TDetail>(t.Item1, t.Item2, t.Item3);
  203.         }
  204.  
  205.         /// <summary>
  206.         /// Convert a <see cref="TryReturn{T, TDetail}" /> to
  207.         /// the equivalent <see cref="System.Tuple{Boolean, T, TDetail}" />
  208.         /// </summary>
  209.         /// <param name="twr"><see cref="TryReturn{T, TDetail}" /> to convert.</param>
  210.         /// <returns><paramref name="twr" /> as the equivalent <see cref="System.Tuple{Boolean, T, TDetail}" />.</returns>
  211.         public static implicit operator Tuple<bool, T, TDetail>(TryReturn<T, TDetail> twr)
  212.         {
  213.             return new Tuple<bool, T, TDetail>(twr.Result, twr.TypeResult, twr.DetailTypeResult);
  214.         }
  215.  
  216.         /// <summary>
  217.         /// Convert a <see cref="TryReturn{T, TDetail}" /> to the equivalent <see cref="System.Boolean" />,
  218.         /// corresponding to the <see cref="ITryReturn{T}.Result" /> value
  219.         /// </summary>
  220.         /// <param name="twr"><see cref="TryReturn{T, TDetail}" /> to convert.</param>
  221.         /// <returns><paramref name="twr" /> as the equivalent <see cref="System.Boolean" />.</returns>
  222.         public static implicit operator bool(TryReturn<T, TDetail> twr)
  223.         {
  224.             return twr.Result;
  225.         }
  226.     }
  227.  
  228.     /// <summary>
  229.     /// Factory for representations of results of methods that can fail
  230.     /// </summary>
  231.     public static class TryReturn
  232.     {
  233.         #region Static methods
  234.  
  235.         /// <summary>
  236.         /// Create a try-return with default values.
  237.         /// </summary>
  238.         /// <typeparam name="T">Typed result returned by the operation that can fail.</typeparam>
  239.         /// <returns>Try return with result and typed result values set to their defaults.</returns>
  240.         public static TryReturn<T> Create<T>()
  241.         {
  242.             return new TryReturn<T>(); // Default values used
  243.             //return new TryReturn<T>(false, default(T));
  244.         }
  245.  
  246.         /// <summary>
  247.         /// Create a try-return from the <paramref name="typeResult" />.
  248.         /// </summary>
  249.         /// <typeparam name="T">Typed result returned by the operation that can fail.</typeparam>
  250.         /// <param name="typeResult">Type result returned from the try.</param>
  251.         /// <returns>Try return with typed result value assigned.</returns>
  252.         public static TryReturn<T> Create<T>(T typeResult)
  253.         {
  254.             return new TryReturn<T>(typeResult);
  255.         }
  256.  
  257.         /// <summary>
  258.         /// Create a try-return from the <paramref name="result" /> and <paramref name="typeResult" />.
  259.         /// </summary>
  260.         /// <typeparam name="T">Typed result returned by the operation that can fail.</typeparam>
  261.         /// <param name="result">Boolean result returned from the try (whether try succeeded).</param>
  262.         /// <param name="typeResult">Type result returned from the try.</param>
  263.         /// <returns>Try return with result and typed result values assigned.</returns>
  264.         public static TryReturn<T> Create<T>(bool result, T typeResult)
  265.         {
  266.             return new TryReturn<T>(result, typeResult);
  267.         }
  268.  
  269.         // 2-type:
  270.  
  271.         /// <summary>
  272.         /// Create a try-return with default values.
  273.         /// </summary>
  274.         /// <typeparam name="T">Typed result returned by the operation that can fail.</typeparam>
  275.         /// <typeparam name="TDetail">Detail returned by the operation.</typeparam>
  276.         /// <returns>Try return with result, typed result, and detail values set to their defaults.</returns>
  277.         public static TryReturn<T, TDetail> Create<T, TDetail>()
  278.         {
  279.             return new TryReturn<T, TDetail>(); // Default values used
  280.             //return new TryReturn<T, TDetail>(false, default(T), default(TDetail));
  281.         }
  282.  
  283.         /// <summary>
  284.         /// Create a try-return from the <paramref name="typeResult" />.
  285.         /// </summary>
  286.         /// <typeparam name="T">Typed result returned by the operation that can fail.</typeparam>
  287.         /// <typeparam name="TDetail">Detail returned by the operation.</typeparam>
  288.         /// <param name="typeResult">Typed result returned from the tried operation.</param>
  289.         /// <param name="detailResult">Detail returned from the tried operation.</param>
  290.         /// <returns>Try return with typed result and detail values assigned.</returns>
  291.         public static TryReturn<T, TDetail> Create<T, TDetail>(T typeResult, TDetail detailResult)
  292.         {
  293.             return new TryReturn<T, TDetail>(typeResult, detailResult);
  294.         }
  295.  
  296.         /// <summary>
  297.         /// Create a try-return from the <paramref name="result" /> and <paramref name="typeResult" />.
  298.         /// </summary>
  299.         /// <typeparam name="T">Typed result returned by the operation that can fail.</typeparam>
  300.         /// <typeparam name="TDetail">Detail returned by the operation.</typeparam>
  301.         /// <param name="result">Boolean result returned from the tried operation (whether try succeeded).</param>
  302.         /// <param name="typeResult">Typed result returned from the tried operation.</param>
  303.         /// <param name="detailResult">Detail returned from the tried operation.</param>
  304.         /// <returns>Try return with typed result and detail values assigned.</returns>
  305.         public static TryReturn<T, TDetail> Create<T, TDetail>(bool result, T typeResult, TDetail detailResult)
  306.         {
  307.             return new TryReturn<T, TDetail>(result, typeResult, detailResult);
  308.         }
  309.  
  310.         #endregion
  311.     }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement