/maybe-dotnet

Primary LanguageC#Apache License 2.0Apache-2.0

maybe-dotnet

This project provides a Maybe<T> struct which can be used to help clarify and handle cases when methods may return no value instead of returning null. This blog post explains the motivations for why you would use it.

Examples

These examples assume that you have an Account class and a repository for finding and saving accounts.

Creating maybes

var some = Maybe<Account>.Some(new Account());
var none = Maybe<Account>.None;
var someOther = account.ToMaybe(); // will be some or none depending on whether the account was null.

Checking if the maybe has value

var foundAccount = maybeAccount.HasValue;

Getting the value from a maybe

var account = maybeAccount.ValueOrDefault(new Account());
var account = maybeAccount.ValueOrThrow(new Exception("No account was found"));

Performing an action if there is a value

var maybeAccount = repository.Find(accountId);
maybeAccount.IfSome(account =>
{
    account.LastUpdated = DateTimeOffset.UtcNow;
    repository.Save(account);
});

Handle the cases where there is some value or there is none

var name = maybeAccount.Case(
    some: account => account.FirstName,
    none: () => "Anonymous");

Map a maybe to another type

Maybe<string> maybeFirstName = maybeAccount.Map(account => account.FirstName);
Maybe<IList<string>> emails = maybeAccount.Map(account => repository.GetEmailAddresses(account));