NOption 1.1.0

Implementation of the Option pattern (also called Maybe pattern) for the .NET platform

There is a newer version of this package available.
See the version list below for details.
Install-Package NOption -Version 1.1.0
dotnet add package NOption --version 1.1.0
<PackageReference Include="NOption" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NOption --version 1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

NOption

NOption is a concise and fluent implementation of the Option pattern for the .NET platform.

Usage

Add the library namespace

Simply import the NOption namespace:

import NOption;

Use Option<> as the return type

To start using NOption:

  • Change your function return type to Option<>;
  • The return value will implicitely be wrapped in an Option<>;
  • To specify that there is no return value, use Option.None.
static Option<Color> GetFavoriteColor(string username)
{
	if ("Emma".Equals(username))
	{
		return Color.Blue;
	}
	return Option.None;
}

Working with the result

To work with result, you can use Match (with named arguments to improve readability):

var result = GetFavoriteColor("Emma");
result.Match(
	Some: (favoriteColor) => Console.WriteLine($"Emma likes the color {favoriteColor}."),
	None: () => Console.WriteLine("Emma does not have a favorite color.")
);
// Print "Emma likes the color Blue"

You can also unwrap the content with a default value:

var result = GetFavoriteColor("Mark");
var favoriteColor = result.UnwrapOr(Color.Purple); // Returns Color.Purple

Or you can extract the value:

var result = GetFavoriteColor("Emma");
if (result.HasSome(out var favoriteColor))
{
 Console.WriteLine($"Emma likes the color {favoriteColor}.")
}

Bonus

To rewrite GetFavoriteColor as a one line function, we could have use:

static Option<Color> GetFavoriteColor(string username)
{
  return "Emma".Equals(username) ? Option.Some(Color.Blue) : Option.None;
}

NOption

NOption is a concise and fluent implementation of the Option pattern for the .NET platform.

Usage

Add the library namespace

Simply import the NOption namespace:

import NOption;

Use Option<> as the return type

To start using NOption:

  • Change your function return type to Option<>;
  • The return value will implicitely be wrapped in an Option<>;
  • To specify that there is no return value, use Option.None.
static Option<Color> GetFavoriteColor(string username)
{
	if ("Emma".Equals(username))
	{
		return Color.Blue;
	}
	return Option.None;
}

Working with the result

To work with result, you can use Match (with named arguments to improve readability):

var result = GetFavoriteColor("Emma");
result.Match(
	Some: (favoriteColor) => Console.WriteLine($"Emma likes the color {favoriteColor}."),
	None: () => Console.WriteLine("Emma does not have a favorite color.")
);
// Print "Emma likes the color Blue"

You can also unwrap the content with a default value:

var result = GetFavoriteColor("Mark");
var favoriteColor = result.UnwrapOr(Color.Purple); // Returns Color.Purple

Or you can extract the value:

var result = GetFavoriteColor("Emma");
if (result.HasSome(out var favoriteColor))
{
 Console.WriteLine($"Emma likes the color {favoriteColor}.")
}

Bonus

To rewrite GetFavoriteColor as a one line function, we could have use:

static Option<Color> GetFavoriteColor(string username)
{
  return "Emma".Equals(username) ? Option.Some(Color.Blue) : Option.None;
}

  • .NETStandard 2.0

    • No dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.1.1 110 9/2/2019
1.1.0 81 9/2/2019
1.0.0 122 8/11/2019
0.2.0 140 5/27/2019