Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8988781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:05:25+00:00 2026-06-15T22:05:25+00:00

I read about monadic errors as being an alternative to return value errors and

  • 0

I read about monadic errors as being an alternative to return value errors and exceptions. Can someone explain and give an example of how monadic errors would work in an imperative pseudo-language (no functional examples please).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:05:26+00:00Added an answer on June 15, 2026 at 10:05 pm

    The basic idea:

    • the notion of success with output or fail with error is encoded into a type.
    • Values (or functions) of this type can be composed into another value of this type. Values with different success types can be composed, but they must all have the same fail type.
    • Once an error is output, the rest of the execution is short-circuited to propagate the error.

    Here is a working example in C#:


    // A discrimated union that can be either an error of type TError, 
    // or a successful result of type TResult. 
    // IsError indicates which field has a valid value.
    class Throws<TError, TResult>
    {
        public bool IsError;
        public TError Error;
        public TResult Result;
    
        // Make a new successful reslt of type TResult.
        public static Throws<TError, TResult> Success(TResult result)
        {
            Throws<TError, TResult> t = new Throws<TError, TResult>();
            t.IsError = false;
            t.Result = result;
            return t;
        }
    
        // Make a new error of type TError.
        public static Throws<TError, TResult> Fail(TError error)
        {
            Throws<TError, TResult> t = new Throws<TError, TResult>();
            t.IsError = true;
            t.Error = error;
            return t;
        }
    
        // Composition.
        public Throws<TError, TResultB> Bind<TResultB>(
                  Func<TResult, Throws<TError, TResultB>> f)
        {
            if (IsError)
            {
                // If this is an error, then we can short circuit the evaluation
                return Throws<TError, TResultB>.Fail(Error);
            }
    
            // Otherwise, forward this result to the next computation.
            return f(Result);
        }
    }
    
    class Test
    {
        // num / demom
        private static Throws<string, double> Div(double num, double denom)
        {
            if (denom == 0)
                return Throws<string, double>.Fail("divide by zero");
    
            return Throws<string, double>.Success(num / denom);
        }
    
        // Have the user enter a double.
        private static Throws<string, double> ReadDouble(string name)
        {
            Console.Write("{0}: ", name);
    
            string input = Console.ReadLine();
            double result;
            if (!double.TryParse(input, out result))
                return Throws<string, double>.Fail(string.Format("can't parse {0}", name));
    
            return Throws<string, double>.Success(result);
        }
    
        // Read two doubles and divide them to produce the result.
        private static Throws<string, double> Interact()
        {
            return ReadDouble("numerator").Bind(num => 
                   ReadDouble("denominator").Bind(denom => 
                   Div(num, denom)));
        }
    
        public static void TestLoop()
        {
            while (true)
            {
                // Run a computation that asks the user for two numbers,
                // divides them and then prints out the result.
                Throws<string, double> t = Interact();
    
                // Notice how the return type forces you to address the
                // error if you want to get to the value.
                if (t.IsError)
                {
                    Console.WriteLine("Error: {0}", t.Error);
                }
                else
                {
                    Console.WriteLine("Success: {0}", t.Result);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read about implicit conversions being done between different value class types. The book
I read about small talk being completely object oriented.. is C++ also completely object
I read about ContactsContract.CommonDataKinds.GroupMembership , but I can't figure out what URI use to
Read about the z-index issue and tried anything, but just can't seem to fix
i read about how to use the <@Html.AntiForgeryToken()> to generate an encrypted value in
I read about structure in c++ that it can not contain instance of itself.
I read about templated controls/INamingContainer but I can't understand what problem it solves when
I read about partial classes and, for example, I understand the reason for they
I read about IS OF TYPE and I expected that it should return TRUE,
I read about lexical-let's memory leak, for example here: Are there any problems with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.