Skip to content
BoogeyCodesPublic

About

Lightweight Result and Option functional types for C#: Map, Bind, Match, no null or exceptions for expected failures

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Outcome

CI .NET 8 License: MIT

Lightweight Result and Option types for C#. Model "a value that might be missing" and "an operation that might fail" in the type system instead of with null and exceptions — with Map, Bind and Match to compose them.

using Outcome;

Result<int, string> ParseAge(string s) =>
    int.TryParse(s, out var n)
        ? n >= 0 ? Result.Ok<int, string>(n) : Result.Err<int, string>("negative")
        : Result.Err<int, string>("not a number");

string message = ParseAge("42")
    .Map(age => age + 1)
    .Bind(age => age < 150 ? Result.Ok<int, string>(age) : Result.Err<int, string>("implausible"))
    .Match(age => $"next year you'll be {age}", err => $"invalid: {err}");

Why

null and exceptions are the two usual ways to say "there might not be a value" or "this might fail", and both are easy to get wrong: a null slips past the compiler, and exceptions for expected failures are heavy and easy to forget to catch. Option<T> and Result<T, TError> put both cases in the type, so the compiler makes you handle them, and Map/Bind/Match let you chain operations where an error or a missing value short-circuits the rest. This is a small, focused implementation of that pattern — two structs and their combinators.

Option<T>

A value that is present (Some) or absent (None).

Option<User> FindUser(int id) => /* ... */;

string name = FindUser(42)
    .Map(u => u.Name)
    .Where(n => n.Length > 0)
    .GetValueOr("(unknown)");

// implicit conversions and null-bridging
Option<string> a = "hello";              // Some
Option<string> b = Option.From(maybeNull); // None if null, else Some
if (FindUser(1).TryGetValue(out var user)) { /* ... */ }

Members: IsSome/IsNone, Match, Map, Bind, Where, GetValueOr, TryGetValue, ToResult(error), plus Option.Some/None/From and implicit conversion from a value.

Result<T, TError>

A success (Ok) or a typed failure (Err).

Result<Config, string> result = LoadConfig(path);

result
    .Map(cfg => cfg.Port)
    .MapErr(err => $"config error: {err}")
    .Match(port => Listen(port), err => Log(err));

// turn an exception-throwing call into a Result
Result<int, Exception> parsed = Result.Try(() => int.Parse(text));

Members: IsOk/IsErr, Match (function or action), Map, MapErr, Bind, GetValueOr, TryGetValue, ToOption, plus Result.Ok/Err/Try.

Both types are immutable readonly structs with value equality.

Install

Packaged as a .NET library (Outcome.FunctionalTypes). Reference the project, or build the NuGet package with dotnet pack.

Testing

dotnet test

Eighteen tests covering construction, Match, Map/Bind/MapErr, filtering, fallbacks, null-bridging and implicit conversion, Try capturing exceptions, the Option/Result conversions, TryGetValue, value equality, and a composed pipeline. CI runs on Linux and Windows and builds the NuGet package.

Project layout

src/Outcome
├── Option.cs   Option<T> and the Option factory
└── Result.cs   Result<T, TError> and the Result factory
tests/Outcome.Tests  xUnit tests

Ideas for later

  • Async combinators (MapAsync, BindAsync).
  • LINQ query-syntax support (Select/SelectMany).
  • A Result<T> with a built-in error type for the common case.

License

MIT

About

Lightweight Result and Option functional types for C#: Map, Bind, Match, no null or exceptions for expected failures

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages