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 6670423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:16:02+00:00 2026-05-26T03:16:02+00:00

I’ve been trying to figure out how I could use the Maybe monad in

  • 0

I’ve been trying to figure out how I could use the Maybe monad in iSynaptic.Commons in a context where my value retriever could throw an exception:

For example:

dynamic expando = new Expando();
expando.Name = "John Doe";

var maybe = Maybe.Defer(()=>(string)expando.NonExistingProperty);

//In this context I would like the exception which is thrown 
//to result in Maybe<string>.NoValue;
if(maybe.HasValue) {
    //Do something
}

Is this possible with the implementation of maybe that is out there

  • 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-05-26T03:16:03+00:00Added an answer on May 26, 2026 at 3:16 am

    There are several ways of using iSynaptic.Commons to allow an exception. Each way I found requires the .Catch() extension method to let the monad know to silently catch the exception. Also, be careful when accessing the property maybe.Value. If this property is Maybe.NoValue, an InvalidOperationException will be thrown.


    1) Create a “OnExceptionNoValue” extension method. This will check the Maybe to see if it has an exception. If it does, a NoValue Maybe will be returned. Otherwise the original Maybe will be returned.

    public static class MaybeLocalExtensions
    {
        public static Maybe<T> OnExceptionNoValue<T>(this Maybe<T> maybe)
        {
            return maybe.Exception != null ? Maybe<T>.NoValue : maybe;
        }
    }
    
    // Sample Use Case:
    var maybe = Maybe.Defer(() => (string)expando.NonExistingProperty).Catch()
        .OnExceptionNoValue();
    

    2) Create a “BindCatch” extension method. This changes the behavior of the normal bind when an exception is present to return Maybe.NoValue instead of throwing an exception.

    public static class MaybeLocalExtensions
    {
        public static Maybe<TResult> BindCatch<T, TResult>(this Maybe<T> @this, Func<T, Maybe<TResult>> selector)
        {
            var self = @this;
    
            return new Maybe<TResult>(() => {
                if (self.Exception != null)
                    return Maybe<TResult>.NoValue;
    
                return self.HasValue ? selector(self.Value) : Maybe<TResult>.NoValue;
            });
        }
    }
    
    // Sample Use Case:
    var maybe = Maybe.Defer(() => (string)expando.NonExistingProperty).Catch()
        .BindCatch(m => m.ToMaybe());
    

    3) This way also uses the Catch() extension method, but uses the maybe.HasValue property instead of relying on extension methods. If an exception is present in the Maybe, the HasValue property is false. When this value is false, the Maybe.NoValue can replace the value of the variable maybe or whatever needs to be done in this case.

    dynamic expando = new ExpandoObject();
    expando.Name = "John Doe";
    
    // This example falls to the else block.
    var maybe = Maybe.Defer(() => (string)expando.NonExistingProperty).Catch();
    
    //In this context I would like the exception which is thrown 
    //to result in Maybe<string>.NoValue;
    if (maybe.HasValue) {
        //Do something
        Console.WriteLine(maybe.Value);
    } else {
        maybe = Maybe<string>.NoValue; // This line is run
    }
    
    // This example uses the if block.
    maybe = Maybe.Defer(() => (string)expando.Name).Catch();
    //to result in Maybe<string>.NoValue;
    if (maybe.HasValue) {
        //Do something
        Console.WriteLine(maybe.Value); //This line is run
    } else {
        maybe = Maybe<string>.NoValue;
    }
    

    These answers are all variations on the same theme, but I hope they are helpful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
This could be a duplicate question, but I have no idea what search terms

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.