Bad news. I have moved on to an entirely different platform (iOS/Swift) and will no longer be maintaining this library.
The good news is that Microsoft is introducing nullable reference types in C# 8. That should make libraries like this one less important. Although, I believe that they still have a place.
No one likes NullReferenceExceptions. Let's change things. Let consumers of your code know when a return value might be empty. Force them to acknowledge a possible lack of value.
My goal is to make Maybe
as easy to use as possible, but also force the consuming code down the path of success. I'm doing this by not including nice to have features such as implicit conversions, .HasValue
or .Value
, etc.
When you're returning a value that might be null, follow this style:
public Maybe<Person> GetPersonByName(string firstName, string lastName)
{
var person = _database.People
.Where(x => x.FirstName == firstName && x.LastName == lastName)
.FirstOrDefault()
.ToMaybe();
return person;
}
You're consumer will only be able to execute code on the value through accessor methods:
GetPersonByName("John", "Doe")
.Do(person => SomeUsefulAction(person))
.DoWhenEmpty(() => this.Log().Warn("No user named 'John Doe' could be found."));
You're welcome to install this library, but keep in mind it is a pre-release. I'm still plying with the API surface. Things might get renamed or dissapeared.
Install-Package SpicyTaco.Maybe -Pre
I've licensed this under MIT License. That means do what you want with this code.