Example #1
0
        // This method handles the fallible result, so would normally only be called in the client
        // C#7 version
        //public void Match(Action<T> onSuccess, Action<string, string> onFailure, Action<string, string> onBadIdea = null) {
        //  switch (this) {
        //    case Success<T> success:
        //      onSuccess(success.Value);
        //      break;
        //    case BadIdea<T> badIdea:
        //      if (onBadIdea != null) {
        //        onBadIdea(badIdea.Message, badIdea.StackTrace);
        //      } else {
        //        onFailure(badIdea.Message, badIdea.StackTrace);
        //      }
        //      break;
        //    case Failure<T> failure:
        //      // TODO AYS - Handle exception
        //      onFailure(failure.Message, failure.StackTrace);
        //      break;
        //  }
        //}

        // C#6 version
        public void Match(Action <T> onSuccess, Action <string> onFailure, Action <string> onBadIdea = null)
        {
            if (this is Success <T> )
            {
                Success <T> success = (Success <T>) this;
                onSuccess(success.Value);
            }
            else if (this is BadIdea <T> )
            {
                BadIdea <T> badIdea = (BadIdea <T>) this;
                if (onBadIdea != null)
                {
                    onBadIdea(badIdea.Message);
                }
                else
                {
                    onFailure(badIdea.Message);
                }
            }
            else if (this is Failure <T> )
            {
                Failure <T> failure = (Failure <T>) this;
                onFailure(failure.Message);
            }
        }
Example #2
0
        private Fallible <T> DoPrivate(Func <T> f)
        {
            Fallible <T> result;

            // Create a logger based on the call stack, as that way we can set the type to be the class that made the call to Do()
            try {
                T fResult = f();
                result = new Success <T> {
                    Value = fResult
                };
            }
            catch (BadIdeaException ex) {
                result = new BadIdea <T> {
                    Message = ex.Message, StackTrace = ex.StackTrace
                };
            }
            catch (Exception ex) {
                result = new Failure <T> {
                    Message = ex.Message, StackTrace = ex.StackTrace
                };
            }
            return(result);
        }