An open API service indexing awesome lists of open source software.

https://github.com/neo-ciber94/extrautilsresult

Provides an implementation of Result, Result<T> and Result<T, TError>
https://github.com/neo-ciber94/extrautilsresult

Last synced: 2 months ago
JSON representation

Provides an implementation of Result, Result<T> and Result<T, TError>

Awesome Lists containing this project

README

          

# ExtraUtils.Result
Provides an implementation of the functional type Result as
``Result``, ``Result`` and ``Result`` the represent
the result of an operator that can be either a successful value or an error.

## Implementation
Each result type inherit from ``IResult``.

```csharp
public interface IResult
{
public bool IsSuccess { get; }
public bool IsError { get; }
}
```

And provides methods for *get*, *check* or *transform* the result and error.

## Usage
Import the namespace
```csharp
using ExtraUtils;
```

A ``Result`` should be created using ``Result.Ok(...)`` for success
or ```Result.Error(...)``` for errors.

```csharp
public static Result FindById(int id)
{
if(Database.TryGetValue(out Person person))
{
return Result.Ok(person);
}

return Result.Error($"Cannot find person with id {id}");
}
```

Then using the ``Result``
```csharp
Result result = FindById(138102);

if(result.IsSuccess)
{
// Use value
Person value = result.Value;
}
```

For convenience there is an implicit convention from
``Result`` and ``Result("Invalid result").Value;
```

Also there is a down conversion between ``Result``.
```csharp
Result result1 = Result.Ok(10);
Result result2 = result1;
Result result3 = result2;
```

Also ``Result`` provides an equivalent of pattern matching

```csharp
Result result = FindByID(-12);
string value = result.Match(p => p.Name, e => string.Empty);
```

Or an equivalent and more elegant expression

```csharp
Result result = FindByID(-12);

string value = result.Match(
ok: p => p.Name,
error: e => string.Empty
);
```