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>
- Host: GitHub
- URL: https://github.com/neo-ciber94/extrautilsresult
- Owner: Neo-Ciber94
- Created: 2020-01-18T02:54:39.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-01-19T15:22:39.000Z (over 5 years ago)
- Last Synced: 2024-12-28T12:30:48.022Z (9 months ago)
- Language: C#
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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
);
```