https://github.com/emik03/emik.results
Contains the Result type; A type representing either a success value or failure value.
https://github.com/emik03/emik.results
csharp csharp-library dll
Last synced: 6 months ago
JSON representation
Contains the Result type; A type representing either a success value or failure value.
- Host: GitHub
- URL: https://github.com/emik03/emik.results
- Owner: Emik03
- License: mpl-2.0
- Created: 2022-11-22T13:50:09.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-15T21:40:28.000Z (almost 2 years ago)
- Last Synced: 2025-10-25T23:55:17.611Z (10 months ago)
- Topics: csharp, csharp-library, dll
- Language: C#
- Homepage:
- Size: 354 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Emik.Results
[](https://www.nuget.org/packages/Emik.Results)
[](https://github.com/Emik03/Emik.Results/blob/main/LICENSE)
`Result` 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](https://github.com/Emik03/Emik.Morsels), if you are building this project, refer to its [README](https://github.com/Emik03/Emik.Morsels/blob/main/README.md) first.
---
- [Examples](#examples)
- [Contribute](#contribute)
- [License](#license)
---
## Examples
```csharp
using System;
using System.Linq;
using Emik.Results;
using static Emik.Results.Please;
T Throw(T unused) => throw new();
// Turn try-catch into a Result, and/or use operators to transform them.
Result 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', allowing for LINQ usage.
int length = fail.Select(x => x.Length).ToArray();
// Implements 'ISet' as well, allowing for quering of other 'IEnumerable' instances.
var thisIsTrue = Result.Ok(2).IsSupersetOf([2]);
```
```csharp
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 success = await Task.FromResult(10).Try();
// Works with Lazy.
Result failure = new Lazy(() => throw new()).Try();
// Convert nullable value types into Result.
Result none = default(int?).IntoOk();
// Convert nullable reference types into Result.
Result some = "".IntoOk();
// Filter and map.
var thisIsAlsoTrue = Err(2).MapErr(x => new List(x)).IsErrAnd(x => x is { Capacity: 2, Count: 0 });
```
For a list of APIs, [click here](https://github.com/Emik03/Emik.Results/blob/main/Documentation/index.md).
## 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](https://www.mozilla.org/en-US/MPL/2.0/).