7

It's 2015, is there any "official" maybe monad in C#? Ideally, it would work something ala Scala's Option, Some and None types.

C# seems to have everything needed, i.e. co/contravariance and lambdas.

I'm asking this because I recently started working in a company that uses Unity, and I run into a lot of delayed initialization. In an attempt to avoid NullPointerException, I would like to maybe invite them into the monad world. Any ideas on this, or should we simply deal with this in another way?

dbc
  • 104,963
  • 20
  • 228
  • 340
Felix
  • 8,385
  • 10
  • 40
  • 59
  • No, there are no official monads "out of the box", but you can find many implementations on Nuget – Alex Voskresenskiy Mar 18 '15 at 15:54
  • 1
    Check this out: http://mikehadlow.blogspot.co.il/2011/01/monads-in-c-5-maybe.html. Thought I seriously doubt whether this increases readability. I would go for Java's Optional construct instead. – Vitaliy Mar 18 '15 at 15:55
  • http://stackoverflow.com/questions/674855/help-a-c-sharp-developer-understand-what-is-a-monad The accepted answer also links to http://blogs.msdn.com/b/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx – Dennis_E Mar 18 '15 at 15:56
  • 1
    Ignoring (at my peril) your specific request for monad, for "a lot of delayed initialization", any chance you just need to use the `Lazy` class? Your question is missing a lot of context that would help people understand what specific _problem_ you're actually trying to solve. Please see http://stackoverflow.com/help/how-to-ask, as well as http://stackoverflow.com/help/mcve. – Peter Duniho Mar 18 '15 at 16:01
  • 2
    Of course there is such a type - it hides in `FSharp.Core` ;) – Random Dev Mar 18 '15 at 16:15
  • I think you may need to look into how your new company actually handles this delayed initialization currently and see how their current approach works for them – Sayse Mar 18 '15 at 16:15
  • Dennis_E> The "monadsinc5" one is great. I think SelectMany and GetEnumerator are what I need to use a maybe-like monad effectively. In the end, what I needed was just a way to push the possibility of a null-value into the type system and a way to manipulate them without checking for null all the time. – Felix Mar 20 '15 at 13:37
  • This is my implementation: https://github.com/gsscoder/csharpx. – gsscoder Jul 20 '20 at 16:16

1 Answers1

1

As far as "official" approaches go, remember that if you're using C# 6, you can use the null-conditional operator:

var myVal = possiblyNull?.thisToo?.andThis?.value;

Certain VisualStudio templates (such as the ASP.NET MVC project template) also include the IsNotNull extension method, which is part of the AjaxMinExtensions. If you don't have those in your project, you could copy/paste the implementation into your code.

var myVal = possiblyNull.IfNotNull(v => v.doTheThing())
                        .IfNotNull(theThing => theThing.TheProperty);
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • 4
    while beeing called a "monadic" operator that is not quite the same - if you go with a `Maybe` or `Option` type the compiler will help you see the places where you missed handling possible null values (by giving you errors) – Random Dev Mar 18 '15 at 16:17
  • @CarstenKönig Yes, certainly true. – JLRishe Mar 18 '15 at 16:18