Skip to content

Emik03/Emik.Results

Repository files navigation

Emik.Results

NuGet package License

Result<TOk, TErr> is the type used for returning and propagating errors. It is either Ok, representing success and containing a value, or Err, representing error and containing an error value.

This project has a dependency to Emik.Morsels, if you are building this project, refer to its README first.



Examples

using System;
using System.Linq;
using Emik.Results;
using static Emik.Results.Please;

T Throw<T>(T unused) => throw new();

// Turn try-catch into a Result, and/or use operators to transform them.
Result<string, Exception> fail = "foo" & Try(Throw, "bar");

// 'IsErr' guarantees each variant be non-null in their appropriate branches.
string value = fail.IsErr ? fail.Err.Message : fail.Ok;

// You can also use the out parameter variant.
value = fail.Out(out var str, out var exception) ? str : exception.Message;

// Implements 'IEnumerable<T>', allowing for LINQ usage.
int length = fail.Select(x => x.Length).ToArray();

// Implements 'ISet<T>' as well, allowing for quering of other 'IEnumerable<T>' instances.
var thisIsTrue = Result.Ok(2).IsSupersetOf([2]);
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Emik.Results;
using Emik.Results.Extensions;
using static Emik.Results.Result;

// Works with Tasks and ValueTasks too.
Result<int, Exception> success = await Task<int>.FromResult(10).Try();

// Works with Lazy.
Result<int, Exception> failure = new Lazy<int>(() => throw new()).Try();

// Convert nullable value types into Result.
Result<int, Unit> none = default(int?).IntoOk();

// Convert nullable reference types into Result.
Result<string, Unit> some = "".IntoOk();

// Filter and map.
var thisIsAlsoTrue = Err(2).MapErr(x => new List<int>(x)).IsErrAnd(x => x is { Capacity: 2, Count: 0 });

For a list of APIs, click here.

Contribute

Issues and pull requests are welcome to help this repository be the best it can be.

License

This repository falls under the MPL-2 license.

About

Contains the Result type; A type representing either a success value or failure value.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages